﻿Imports System.Data.SqlClient
Imports System.IO
Public Class clsDeviceConnection
    Dim tblParaName As New DataTable
    Dim vResult(5, 0) As String
    '0: Name; 1: Gia tri
    '2: Don vi; 3: muc thong thuong cho Nam
    '4: Muc thong thuong cho nu
    '5: Data sequence
    'server

    Public Function spClinitek(ByVal strInput As String, _
                               ByVal pTestTypeID As Int16, _
                               ByVal PortNum As Int16) As Boolean
        Dim strResult() As String
        Dim iCount, iPara As Int16
        Dim pTestDate, pTestSeq, strTemp, pBarcode As String

        Try
            File.AppendAllText("C:\LOG\Clinitek_Input.txt", Now.ToString & vbCrLf & strInput & vbCrLf)
            While Not (Char.IsLetterOrDigit(strInput(0)))
                strInput = strInput.Substring(1)
            End While

            'IO.File.WriteAllText("C:\LOG\Clinitek" & "_" & Now.Year.ToString & Now.Month.ToString & Now.Day & "_" & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & Now.Millisecond & ".txt", strInput)
            'strInput = "3333,09-05-2009,10:41AM,,3,,,,,Multistix 10 SG,Not Entered,Not Entered,GLU,Negative,,BIL,Negative,,KET,Negative,,SG ,1.015,,BLD,Negative,,pH ,7.0,,PRO,0.3 g/L,*,UBG,33 umol/L,*,NIT,Negative,,LEU,Ca 15 Leu/uL,*"
            strResult = strInput.Split(",")

            ' Nếu là dữ liệu sai
            If strResult.Length < 12 Then
                Exit Function
            End If

            pTestSeq = strResult(0)
            pTestDate = strResult(1).Trim.Substring(0, 2) & "/" & strResult(1).Trim.Substring(3, 2) & "/" & strResult(1).Trim.Substring(6, 4)

            'pBarcode = strResult(1).Trim.Substring(6, 4) & strResult(1).Trim.Substring(3, 2) & strResult(1).Trim.Substring(0, 2) & strResult(4).Trim
            pBarcode = strResult(1).Trim.Substring(8, 2) & strResult(1).Trim.Substring(3, 2) & strResult(1).Trim.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(strResult(4).Trim)
            strTemp = ""

            If strResult(4).Trim = "" Then
                ' Khong co barcode thi ko cap nhat
                Exit Function
            End If

            If Me.spCheckSequence(pTestSeq, pTestDate) Then
                ' Neu da co Sequence nay va cung dung ngay do thi ko co cap nhat lam cai cua khi gi
                Exit Function
            End If


            ' Loại bỏ các thông số chung
            For iCount = 0 To 11
                strTemp = strTemp & strResult(iCount) & ","
            Next
            strInput = strInput.Substring(strTemp.Length)
            strInput = strInput.Replace(",*", "*,")
            strTemp = ",,"
            strInput = strInput.Replace(",,", ";")

            ' Cắt bỏ dấu , ở cuối
            If strInput.Substring(strInput.Length - 1, 1) = "," Then
                strInput = strInput.Substring(0, strInput.Length - 1)
            ElseIf strInput.Substring(strInput.Length - 1, 1) = "*" Then
                strInput = strInput.Substring(0, strInput.Length - 2)
            End If
            strResult = strInput.Split(";")

            ' Lấy thông tin về Test_ID
            Dim Test_ID, Patient_ID As Int64
            'Dim pResult, pParaName As String
            Dim pTemp() As String
            Dim bSpecial As Boolean
            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so

            tblParaName = Me.spGetParaInfor(PortNum)
            ReDim Me.vResult(5, strResult.Length - 1)
            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    pTemp = strResult(iCount).Split(",")
                    Me.vResult(0, iCount) = pTemp(0)
                    Me.vResult(1, iCount) = pTemp(1)
                    Me.vResult(2, iCount) = ""
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    bSpecial = False
                    pTemp = strResult(iCount).Split(",")
                    Me.vResult(0, iCount) = pTemp(0)
                    Me.vResult(1, iCount) = pTemp(1)

                    If pTemp(1).Length > 0 And pTemp(1).Substring(pTemp(1).Length - 1, 1) = "*" Then
                        bSpecial = True
                    End If

                    If Me.vResult(1, iCount).IndexOf(" ") > -1 Then
                        Me.vResult(1, iCount) = Me.vResult(1, iCount).Substring(0, Me.vResult(1, iCount).LastIndexOf(" ")).Trim
                        If bSpecial Then
                            Me.vResult(1, iCount) = Me.vResult(1, iCount) & " **"
                        End If
                    Else
                        If bSpecial Then
                            Me.vResult(1, iCount) = Me.vResult(1, iCount).Substring(0, Me.vResult(1, iCount).Length - 1) & " **"
                        End If
                    End If

                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount).Trim Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(1, iCount) = Me.vResult(1, iCount).Replace("Ca", "").Trim
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                    'End If


                Next
            End If

            Dim pTestDetailID As Int64
            ' Cập nhật vào CSDL
            For iCount = 0 To strResult.Length - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay thi cap nhat lai
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            Return True
        Catch ex As Exception
            IO.File.AppendAllText("C:\LOG\Clinitek_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
            'seryPorts(PortNum).DiscardInBuffer()
            Return False
        End Try
    End Function

    Public Function spUS1000(ByVal strInput As String, _
                               ByVal pTestTypeID As Int16, _
                               ByVal PortNum As Int16) As Boolean
        Dim strResult() As String
        Dim iCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Integer
        Dim pTestDate, pTestSeq, strTemp, pBarcode As String
        Dim cmd As New SqlCommand
        Dim f As File

        Try
            f.AppendAllText("C:\LOG\US1000_StrInput.txt", Now.ToString & vbCrLf & strInput & vbCrLf)
            strResult = strInput.Split(",")
            pBarcode = Me.spGetBarcode(strResult(3).Substring(3))
            pTestDate = strResult(6)
            pTestDate = pTestDate.Substring(8, 2) & "/" & pTestDate.Substring(5, 2) & "/" & pTestDate.Substring(0, 4)
            pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode
            ReDim Me.vResult(5, 9)

            Me.vResult(0, 0) = "URO"
            If strResult(8).Substring(2, 6).Trim = "normal" Then
                Me.vResult(1, 0) = "NORMAL"
            Else
                Me.vResult(1, 0) = strResult(8).Substring(8, 6).Trim * 17
            End If

            Me.vResult(0, 1) = "BLD"
            If strResult(9).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 1) = "NEGATIVE"
            Else
                If strResult(9).Substring(8, 6).Trim = "0.03" Then
                    Me.vResult(1, 1) = "10"
                ElseIf strResult(9).Substring(8, 6).Trim = "0.06" Then
                    Me.vResult(1, 1) = "20"
                ElseIf strResult(9).Substring(8, 6).Trim = "0.15" Then
                    Me.vResult(1, 1) = "50"
                ElseIf strResult(9).Substring(8, 6).Trim = "0.75" Then
                    Me.vResult(1, 1) = "250"
                Else
                    Me.vResult(1, 1) = strResult(9).Substring(8, 6).Trim
                End If
            End If

            Me.vResult(0, 2) = "BIL"
            If strResult(10).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 2) = "NEGATIVE"
            Else
                Me.vResult(1, 2) = strResult(10).Substring(8, 6).Trim * 17
            End If

            Me.vResult(0, 3) = "KET"
            If strResult(11).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 3) = "NEGATIVE"
            Else
                Me.vResult(1, 3) = strResult(11).Substring(2, 6).Trim
            End If

            Me.vResult(0, 4) = "GLU"
            If strResult(12).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 4) = "NEGATIVE"
            Else
                Me.vResult(1, 4) = strResult(12).Substring(8, 6).Trim * 0.0555
            End If

            Me.vResult(0, 5) = "PRO"
            If strResult(13).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 5) = "NEGATIVE"
            Else
                Me.vResult(1, 5) = strResult(13).Substring(8, 6).Trim / 100
            End If

            Me.vResult(0, 6) = "pH"
            Me.vResult(1, 6) = strResult(14).Substring(2, 6).Trim

            Me.vResult(0, 7) = "NIT"
            If strResult(15).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 7) = "NEGATIVE"
            ElseIf strResult(15).Substring(2, 6).Trim = "+" Then
                Me.vResult(1, 7) = "POSITIVE"
            Else
                Me.vResult(1, 7) = strResult(15).Substring(2, 6).Trim
            End If

            Me.vResult(0, 8) = "LEU"
            If strResult(16).Substring(2, 6).Trim = "-" Then
                Me.vResult(1, 8) = "NEGATIVE"
            Else
                Me.vResult(1, 8) = strResult(16).Substring(8, 6).Trim
            End If

            Me.vResult(0, 9) = "S.G"
            Me.vResult(1, 9) = strResult(17).Substring(2, 6).Trim

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)


            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If

            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            'f.AppendAllText("C:\LOG\US1000_Success.txt", strInput & vbCrLf)
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\US1000_Error.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
            Return False
        End Try
    End Function

    Public Function spMythic18(ByVal strInput As String, _
                                    ByVal pTestTypeID As Int16, _
                                    ByVal PortNum As Int16) As Boolean
        Dim f As File
        Dim pTestDate, pBarcode, pTestSEQ As String
        Dim strALL, strResult, strtemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, icount, iParaPos, iPos, iStart, iPara As Int16

        f.AppendAllText("C:\LOG\Mythic18_Input.txt", Now.ToString & vbCrLf & strInput & vbCrLf)

        strInput = strInput.Trim
        Try
            strALL = Split(strInput, "MYTHIC")

            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                ReDim strResult(0)
                If InStr(strALL(iPatientNumber).ToUpper, "WBC") > 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "RBC") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "HGB") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "HCT") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "PLT") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "LYM") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "MON") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "GRA") >= 0 _
                Then
                    ' Neu co du lieu thi moi xet

                    'strtemp = Split(strALL(iPatientNumber), Chr(13))
                    strtemp = Split(strALL(iPatientNumber), vbCr)

                    icount = 0

                    'Do While icount < strtemp.GetLength(0) - 1
                    For icount = 0 To strtemp.GetLength(0) - 1
                        If (strtemp(icount) <> vbCrLf) _
                            And (strtemp(icount).Trim <> "") _
                            And (strtemp(icount).Trim <> Chr(ETX)) _
                            And (Not (strtemp(icount).Trim Is Nothing)) _
                            And (strtemp(icount).Trim <> Chr(STX)) _
                        Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else 'If strResult Then
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If

                            strResult(strResult.GetLength(0) - 1) = strtemp(icount)
                        End If
                    Next icount
                    'icount += 1
                    'Loop

                    pTestDate = strResult(2).Substring(5).Trim
                    'pTestDate = pTestDate.Replace("-", "/")
                    'pTestDate = pTestDate.Insert(pTestDate.Length - 2, "20")
                    pBarcode = strResult(9).Substring(3).Trim

                    'pBarcode = strResult(2).Substring(strResult(2).IndexOf("=") + 1).Trim

                    ' If pBarcode.Length <= 4 Then

                    pBarcode = spGetBarcode(pBarcode)
                    pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = pBarcode.Substring(2)
                    'End If



                    ReDim Me.vResult(5, 17)
                    Dim hehehe As Integer
                    For hehehe = 0 To 17
                        strtemp = strResult(13 + hehehe).Split(";")
                        Me.vResult(0, hehehe) = strtemp(0)
                        Me.vResult(1, hehehe) = strtemp(1).Trim
                    Next hehehe


                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)


                    If Me.tblParaName Is Nothing Then
                        For icount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, icount) = ""
                            Me.vResult(4, icount) = ""
                            Me.vResult(5, icount) = 0
                        Next
                    Else
                        For icount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, icount).Trim.ToUpper Then
                                    Me.vResult(0, icount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, icount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, icount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, icount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, icount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For icount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, icount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, icount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, icount), pBarcode, Me.vResult(0, icount), Me.vResult(1, icount), Me.vResult(2, icount), Me.vResult(3, icount), Me.vResult(4, icount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL

                End If
            Next

            Return True

        Catch ex As Exception
            f.AppendAllText("C:\LOG\Mythic18_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try
    End Function

    Public Function spAdvantus(ByVal strInput As String, _
                                     ByVal pTestTypeID As Int16, _
                                     ByVal PortNum As Int16) As Boolean
        Dim f As File
        Dim pTestDate, pBarcode, pTestSEQ As String
        Dim strALL, strResult, strtemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, icount, iParaPos, iPos, iStart, iPara As Int16

        f.AppendAllText("C:\LOG\Advantus_Input.txt", Now.ToString & vbCrLf & strInput & vbCrLf)

        strInput = strInput.Substring(strInput.IndexOf(Chr(STX)), strInput.Length - strInput.IndexOf(Chr(STX)))
        Try
            strALL = Split(strInput, Chr(STX) & "#")
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                If InStr(strALL(iPatientNumber).ToUpper, "GLU") > 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "BIL") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "KET") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "SG") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "BLD") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "PH") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "PRO") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "UBG") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "NIT") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "LEU") >= 0 _
                Then
                    ' Neu co du lieu thi moi xet

                    'strtemp = Split(strALL(iPatientNumber), Chr(13))
                    strtemp = Split(strALL(iPatientNumber), vbCrLf)
                    icount = 0

                    Do While icount < strtemp.GetLength(0)
                        If (strtemp(icount) <> vbCrLf) _
                            And strtemp(icount).Trim <> "" _
                            And strtemp(icount).Trim <> Chr(ETX) _
                            And strtemp(icount).Trim <> Chr(STX) _
                        Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else 'If strResult Then
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If

                            strResult(strResult.GetLength(0) - 1) = strtemp(icount)
                        End If
                        icount += 1

                    Loop

                    pTestDate = strResult(0).Substring(strResult(0).Length - 8)
                    pTestDate = pTestDate.Replace("-", "/")
                    pTestDate = pTestDate.Insert(pTestDate.Length - 2, "20")
                    pBarcode = strResult(1).Substring(strResult(1).IndexOf("=") + 1).Trim

                    If pBarcode.Length <= 4 Then
                        pBarcode = spGetBarcode(pBarcode)
                        pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                        pBarcode = pBarcode.Substring(2)
                    End If



                    ReDim Me.vResult(5, 9)

                    'Me.vResult(0, 0) = "COLOR"
                    'Me.vResult(1, 0) = strResult(2).Substring(strResult(2).IndexOf(":") + 1).Trim

                    'Me.vResult(0, 1) = "CLARITY"
                    'Me.vResult(1, 1) = strResult(3).Substring(strResult(3).IndexOf(":") + 1).Trim

                    'Me.vResult(0, 2) = "GLU"
                    Me.vResult(0, 0) = strResult(4).Substring(0, 3)
                    Me.vResult(1, 0) = strResult(4).Substring(4).Trim

                    'Me.vResult(0, 3) = "BIL"
                    Me.vResult(0, 1) = strResult(5).Substring(0, 3)
                    Me.vResult(1, 1) = strResult(5).Substring(4).Trim

                    'Me.vResult(0, 4) = "KET"
                    Me.vResult(0, 2) = strResult(6).Substring(0, 3)
                    Me.vResult(1, 2) = strResult(6).Substring(4).Trim

                    'Me.vResult(0, 5) = "SG"
                    Me.vResult(0, 3) = strResult(7).Substring(0, 3)
                    strtemp = strResult(7).Trim.Substring(3).Trim.Split(" ")
                    Me.vResult(1, 3) = strtemp(0).Trim

                    'Me.vResult(0, 6) = "BLD"
                    Me.vResult(0, 4) = strResult(8).Substring(0, 3)
                    Me.vResult(1, 4) = strResult(8).Substring(4).Trim

                    'Me.vResult(0, 7) = "pH"
                    Me.vResult(0, 5) = strResult(9).Substring(0, 3)
                    strtemp = strResult(9).Trim.Substring(4).Trim.Split(" ")
                    Me.vResult(1, 5) = strtemp(0).Trim

                    'Me.vResult(0, 8) = "PRO"
                    Me.vResult(0, 6) = strResult(10).Substring(0, 3)
                    Me.vResult(1, 6) = strResult(10).Substring(4).Trim

                    'Me.vResult(0, 9) = "UBG"
                    Me.vResult(0, 7) = strResult(11).Substring(0, 3)
                    strtemp = strResult(11).Trim.Substring(4).Trim.Split(" ")
                    Me.vResult(1, 7) = strtemp(0).Trim
                    Me.vResult(2, 7) = strtemp(1).Trim

                    'Me.vResult(0, 10) = "NIT"
                    Me.vResult(0, 8) = strResult(12).Substring(0, 3)
                    Me.vResult(1, 8) = strResult(12).Substring(4).Trim

                    'Me.vResult(0, 11) = "LEU"
                    Me.vResult(0, 9) = strResult(13).Substring(0, 3)
                    Me.vResult(1, 9) = strResult(13).Substring(4).Trim


                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)


                    If Me.tblParaName Is Nothing Then
                        For icount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, icount) = ""
                            Me.vResult(4, icount) = ""
                            Me.vResult(5, icount) = 0
                        Next
                    Else
                        For icount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, icount).Trim.ToUpper Then
                                    Me.vResult(0, icount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, icount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, icount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, icount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, icount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For icount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, icount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, icount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, icount), pBarcode, Me.vResult(0, icount), Me.vResult(1, icount), Me.vResult(2, icount), Me.vResult(3, icount), Me.vResult(4, icount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL

                End If
            Next

            Return True

        Catch ex As Exception
            f.AppendAllText("C:\LOG\Advantus_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
        End Try
    End Function

    Public Function spH800(ByVal strInput As String, _
                                     ByVal pTestTypeID As Int16, _
                                     ByVal PortNum As Int16) As Boolean
        Dim f As File
        Dim pTestDate, pBarcode, pTestSEQ As String
        Dim strALL, strResult, strtemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, icount, iParaPos, iPos, iStart, iPara As Int16

        'strInput = f.ReadAllText("G:\VietBaIT\Document\Ket noi cac may\H800\H800 mau 10.txt")

        f.AppendAllText("C:\LOG\H800_input.txt", Now.ToString & vbCrLf & strInput & vbCrLf)

        strInput = strInput.Substring(strInput.IndexOf(Chr(STX)), strInput.Length - strInput.IndexOf(Chr(STX)))
        Try
            strALL = Split(strInput, Chr(STX))
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                If InStr(strALL(iPatientNumber).ToUpper, "GLU") > 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "BIL") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "KET") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "SG") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "BLD") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "PH") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "PRO") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "UBG") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "NIT") >= 0 _
                    And InStr(strALL(iPatientNumber).ToUpper, "LEU") >= 0 _
                Then
                    ' Neu co du lieu thi moi xet

                    'strtemp = Split(strALL(iPatientNumber), Chr(13))
                    strtemp = Split(strALL(iPatientNumber), vbCrLf)
                    icount = 0

                    Do While icount < strtemp.GetLength(0)
                        If (strtemp(icount) <> vbCrLf) _
                            And strtemp(icount).Trim <> "" _
                            And strtemp(icount).Trim <> Chr(ETX) _
                            And strtemp(icount).Trim <> Chr(STX) _
                        Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else 'If strResult Then
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If

                            strResult(strResult.GetLength(0) - 1) = strtemp(icount)
                        End If
                        icount += 1

                    Loop

                    pTestDate = strResult(0).Substring(0, 11).Trim
                    pTestDate = pTestDate.Replace("-", "/")
                    'pTestDate = pTestDate.Insert(pTestDate.Length - 2, "20")
                    pBarcode = strResult(2).Substring(5).Trim

                    'pBarcode = strResult(2).Substring(strResult(2).IndexOf("=") + 1).Trim

                    If pBarcode.Length <= 4 Then

                        pBarcode = spGetBarcode(pBarcode)
                        pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                        pBarcode = pBarcode.Substring(2)
                    End If



                    ReDim Me.vResult(5, 9)

                    'Me.vResult(0, 0) = "COLOR"
                    'Me.vResult(1, 0) = strResult(2).Substring(strResult(2).IndexOf(":") + 1).Trim

                    'Me.vResult(0, 1) = "CLARITY"
                    'Me.vResult(1, 1) = strResult(3).Substring(strResult(3).IndexOf(":") + 1).Trim

                    'Me.vResult(0, 2) = "GLU"
                    Me.vResult(0, 0) = strResult(11).Substring(1, 3)
                    Me.vResult(1, 0) = strResult(11).Substring(5).Trim

                    'Me.vResult(0, 3) = "BIL"
                    Me.vResult(0, 1) = strResult(5).Substring(1, 3)
                    Me.vResult(1, 1) = strResult(5).Substring(5).Trim

                    'Me.vResult(0, 4) = "KET"
                    Me.vResult(0, 2) = strResult(6).Substring(1, 3)
                    Me.vResult(1, 2) = strResult(6).Substring(5).Trim

                    'Me.vResult(0, 5) = "SG"
                    Me.vResult(0, 3) = strResult(12).Substring(1, 3)
                    strtemp = strResult(12).Trim.Substring(3).Trim.Split(" ")
                    Me.vResult(1, 3) = strtemp(0).Trim

                    'Me.vResult(0, 6) = "BLD"
                    Me.vResult(0, 4) = strResult(7).Substring(1, 3)
                    Me.vResult(1, 4) = strResult(7).Substring(5).Trim

                    'Me.vResult(0, 7) = "pH"
                    Me.vResult(0, 5) = strResult(13).Substring(1, 3)
                    strtemp = strResult(13).Trim.Substring(5).Trim.Split(" ")
                    Me.vResult(1, 5) = strtemp(0).Trim

                    'Me.vResult(0, 8) = "PRO"
                    Me.vResult(0, 6) = strResult(8).Substring(1, 3)
                    Me.vResult(1, 6) = strResult(8).Substring(5).Trim

                    'Me.vResult(0, 9) = "UBG"
                    Me.vResult(0, 7) = strResult(4).Substring(1, 3)
                    strtemp = strResult(4).Trim.Substring(5).Trim.Split(" ")
                    'Me.vResult(1, 7) = strtemp(0).Trim
                    'Me.vResult(2, 7) = strtemp(1).Trim
                    Me.vResult(1, 7) = strtemp(1).Trim

                    'Me.vResult(0, 10) = "NIT"
                    Me.vResult(0, 8) = strResult(9).Substring(1, 3)
                    Me.vResult(1, 8) = strResult(9).Substring(5).Trim

                    'Me.vResult(0, 11) = "LEU"
                    Me.vResult(0, 9) = strResult(10).Substring(1, 3)
                    Me.vResult(1, 9) = strResult(10).Substring(5).Trim

                    If strResult.Length = 15 Then
                        'Me.vR esult(0, 14) = "VC"
                        ReDim Preserve vResult(5, 10)
                        Me.vResult(0, 10) = strResult(14).Substring(1, 3).Trim
                        Me.vResult(1, 10) = strResult(14).Substring(5).Trim
                    End If

                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)


                    If Me.tblParaName Is Nothing Then
                        For icount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, icount) = ""
                            Me.vResult(4, icount) = ""
                            Me.vResult(5, icount) = 0
                        Next
                    Else
                        For icount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, icount).Trim.ToUpper Then
                                    Me.vResult(0, icount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, icount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, icount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, icount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, icount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For icount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, icount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, icount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, icount), pBarcode, Me.vResult(0, icount), Me.vResult(1, icount), Me.vResult(2, icount), Me.vResult(3, icount), Me.vResult(4, icount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL

                End If
            Next

            Return True

        Catch ex As Exception
            f.AppendAllText("C:\LOG\H800_Error.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
        End Try




    End Function

    Public Function spClinitekADV(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim strResult() As String
        Dim iCount, iPara As Int16
        Dim pTestDate, pTestSeq, strTemp, pBarcode As String
        Dim pTemp() As String
        'Dim cmd As New SqlCommand

        'strInput = f.ReadAllText("C:\LOG\abc.txt")

        If strInput.Substring(0, 1) = Chr(2) Then
            strInput = strInput.Substring(3)
        End If

        If strInput.Substring(strInput.Length - 1) = Chr(3) Then
            strInput = strInput.Substring(0, strInput.Length - 3)
        End If

        strResult = Split(strInput, vbCrLf)
        pTemp = strResult(0).Trim.Split(" ")
        pTestDate = pTemp(pTemp.GetLength(0) - 1).Substring(0, 2) & "/" & pTemp(pTemp.GetLength(0) - 1).Substring(3, 2) & "/" & "20" & pTemp(pTemp.GetLength(0) - 1).Substring(6, 2)
        pBarcode = strResult(1).Substring(3).Trim
        pBarcode = pTestDate.Trim.Substring(6, 4) & pTestDate.Trim.Substring(3, 2) & pTestDate.Trim.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
        pBarcode = pBarcode.Substring(2)

        Dim Test_ID, Patient_ID As Int32
        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
        ' Lay thong tin thong so

        tblParaName = Me.spGetParaInfor(PortNum)
        ReDim Me.vResult(5, strResult.Length - 1)
        If Me.tblParaName Is Nothing Then
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                Me.vResult(2, iCount) = ""
                Me.vResult(3, iCount) = ""
                Me.vResult(4, iCount) = ""
                Me.vResult(5, iCount) = 0
            Next
        Else
            For iCount = 4 To Me.vResult.GetLength(1) - 1


                Me.vResult(0, iCount) = strResult(iCount).Substring(0, strResult(iCount).IndexOf(" ")).Trim
                strResult(iCount) = strResult(iCount).Substring(strResult(iCount).IndexOf(" ")).Trim
                pTemp = strResult(iCount).Split(" ")
                Me.vResult(1, iCount) = pTemp(0)

                For iPara = 0 To Me.tblParaName.Rows.Count - 1
                    If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount).Trim Then
                        Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                        Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                        Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                        Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                        Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                    End If
                Next

            Next
        End If

        Dim pTestDetailID As Int64
        ' Cập nhật vào CSDL
        For iCount = 4 To strResult.Length - 1
            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                ' Neu da co du lieu nay thi cap nhat lai
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
            Else
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
            End If
        Next
        Return True
    End Function

    Public Function spOP151(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim strResult() As String
        Dim iCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim pTestDate, pTestSeq, strTemp, pBarcode As String
        Dim pTemp() As String
        Try
            'strInput = f.ReadAllText("C:\LOG\OP-151.txt")

            strResult = Split(strInput, vbCrLf)

            pTestDate = strResult(0).Trim.Substring(8, 2) & "/" & strResult(0).Trim.Substring(5, 2) & "/" & strResult(0).Trim.Substring(0, 4)
            pBarcode = strResult(2).Trim
            pBarcode = pTestDate.Trim.Substring(6, 4) & pTestDate.Trim.Substring(3, 2) & pTestDate.Trim.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            pBarcode = pBarcode.Substring(2)

            ReDim Me.vResult(5, 9)

            'LEU
            Me.vResult(0, 0) = strResult(4).Substring(1, 3).Trim
            If strResult(4).Substring(5).Trim = "neg." Then
                Me.vResult(1, 0) = "NEGATIVE"
            Else
                Dim Temp_Leu As String()
                Temp_Leu = Split(strResult(4).Substring(5).Trim, " ")
                Me.vResult(1, 0) = Temp_Leu(0)

            End If

            'NIT
            Me.vResult(0, 1) = strResult(5).Substring(1, 3).Trim
            If strResult(5).Substring(5).Trim = "neg." Then
                Me.vResult(1, 1) = "NEGATIVE"
            Else
                Dim Temp_Nit As String()
                Temp_Nit = Split(strResult(5).Substring(5).Trim, " ")
                Me.vResult(1, 1) = Temp_Nit(0)

            End If

            'URO
            Me.vResult(0, 2) = strResult(6).Substring(1, 3).Trim
            If strResult(6).Substring(5).Trim = "neg." Then
                Me.vResult(1, 2) = "NEGATIVE"
            Else
                Dim Temp_Uro As String()
                Temp_Uro = Split(strResult(6).Substring(5).Trim, " ")
                Me.vResult(1, 2) = Temp_Uro(0)
            End If

            'PRO
            Me.vResult(0, 3) = strResult(7).Substring(1, 3).Trim
            If strResult(7).Substring(5).Trim = "neg." Then
                Me.vResult(1, 3) = "NEGATIVE"
            Else
                Dim Temp_Pro As String()
                Temp_Pro = Split(strResult(7).Substring(5).Trim, " ")
                Me.vResult(1, 3) = Temp_Pro(0)

            End If

            'PH
            Me.vResult(0, 4) = strResult(8).Substring(1, 3).Trim
            If strResult(8).Substring(5).Trim = "neg." Then
                Me.vResult(1, 4) = "NEGATIVE"
            Else
                Dim Temp_Ph As String()
                Temp_Ph = Split(strResult(8).Substring(5).Trim, " ")
                Me.vResult(1, 4) = Temp_Ph(0)
            End If

            'BLD
            Me.vResult(0, 5) = strResult(9).Substring(1, 3).Trim
            If strResult(9).Substring(5).Trim = "neg." Then
                Me.vResult(1, 5) = "NEGATIVE"
            Else
                Dim Temp_Bld As String()
                Temp_Bld = Split(strResult(9).Substring(5).Trim, " ")
                Me.vResult(1, 5) = Temp_Bld(0)
            End If

            'SG
            Me.vResult(0, 6) = strResult(10).Substring(1, 3).Trim
            If strResult(10).Substring(5).Trim = "neg." Then
                Me.vResult(1, 6) = "NEGATIVE"
            Else
                Dim Temp_Sg As String()
                Temp_Sg = Split(strResult(10).Substring(5).Trim, " ")
                Me.vResult(1, 6) = Temp_Sg(0)
            End If

            'KET
            Me.vResult(0, 7) = strResult(11).Substring(1, 3).Trim
            If strResult(11).Substring(5).Trim = "neg." Then
                Me.vResult(1, 7) = "NEGATIVE"
            Else
                Dim Temp_Ket As String()
                Temp_Ket = Split(strResult(11).Substring(5).Trim, " ")
                Me.vResult(1, 7) = Temp_Ket(0)
            End If

            'BIL
            Me.vResult(0, 8) = strResult(12).Substring(1, 3).Trim
            If strResult(12).Substring(5).Trim = "neg." Then
                Me.vResult(1, 8) = "NEGATIVE"
            Else
                Dim Temp_Bil As String()
                Temp_Bil = Split(strResult(12).Substring(5).Trim, " ")
                Me.vResult(1, 8) = Temp_Bil(0)
            End If

            'GLU
            Me.vResult(0, 9) = strResult(13).Substring(1, 3).Trim
            If strResult(13).Substring(5).Trim = "neg." Then
                Me.vResult(1, 9) = "NEGATIVE"
            Else
                Dim Temp_Glu As String()
                Temp_Glu = Split(strResult(13).Substring(5).Trim, " ")
                Me.vResult(1, 9) = Temp_Glu(0)
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)


            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If

            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            ' Ket thuc cap nhat CSDL
            f.AppendAllText("C:\LOG\OP151_Input.txt", Now.ToString & vbCrLf & strInput & vbCrLf & vbCrLf)
        Catch ex As Exception
            f.AppendAllText("C:\LOG\OP151_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
            Return False
        End Try
        Return True
    End Function

    Public Function spUrisys1100(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim strResult() As String
        Dim iCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim pTestDate, pTestSeq, strTemp, pBarcode As String
        Dim pTemp() As String
        Try
            'strInput = f.ReadAllText("C:\LOG\Urisys_SID15.txt")
            f.AppendAllText("C:\LOG\Urisys1100_Input.txt", Now.ToString & vbCrLf & strInput & vbCrLf & vbCrLf)
            strResult = Split(strInput, vbLf & vbCr)

            pTestDate = strResult(3).Substring(0, 10)
            pTestDate = pTestDate.Replace(".", "/")
            pBarcode = strResult(5).Substring(7).Trim
            pBarcode = pTestDate.Trim.Substring(6, 4) & pTestDate.Trim.Substring(3, 2) & pTestDate.Trim.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            pBarcode = pBarcode.Substring(2)

            ReDim Me.vResult(5, 9)

            'SG
            Me.vResult(0, 0) = strResult(7).Substring(2, 3).Trim
            If strResult(7).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 0) = "Âm tính"
            Else
                Me.vResult(1, 0) = strResult(7).Substring(5, 7).Trim
            End If

            'pH
            Me.vResult(0, 1) = strResult(8).Substring(2, 3).Trim
            If strResult(8).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 1) = "Âm tính"
            Else
                Me.vResult(1, 1) = strResult(8).Substring(5, 7).Trim
            End If

            'LEU
            Me.vResult(0, 2) = strResult(9).Substring(2, 3).Trim
            If strResult(9).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 2) = "Âm tính"
            Else
                Me.vResult(1, 2) = strResult(9).Substring(5, 7).Trim
            End If

            'NIT
            Me.vResult(0, 3) = strResult(10).Substring(2, 3).Trim
            If strResult(10).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 3) = "Âm tính"
            Else
                Me.vResult(1, 3) = strResult(10).Substring(5, 7).Trim
            End If

            'PRO
            Me.vResult(0, 4) = strResult(11).Substring(2, 3).Trim
            If strResult(11).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 4) = "Âm tính"
            Else
                Me.vResult(1, 4) = strResult(11).Substring(5, 7).Trim
            End If

            'GLU
            Me.vResult(0, 5) = strResult(12).Substring(2, 3).Trim
            If strResult(12).Substring(5, 7).Trim = "norm" Then
                Me.vResult(1, 5) = "Âm tính"
            Else
                Me.vResult(1, 5) = strResult(12).Substring(5, 7).Trim
            End If

            'KET
            Me.vResult(0, 6) = strResult(13).Substring(2, 3).Trim
            If strResult(13).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 6) = "Âm tính"
            Else
                Me.vResult(1, 6) = strResult(13).Substring(5, 7).Trim
            End If

            'UBG
            Me.vResult(0, 7) = strResult(14).Substring(2, 3).Trim
            If strResult(14).Substring(5, 7).Trim = "norm" Then
                Me.vResult(1, 7) = "Bình thường"
            Else
                Me.vResult(1, 7) = strResult(14).Substring(5, 7).Trim
            End If

            'BIL
            Me.vResult(0, 8) = strResult(15).Substring(2, 3).Trim
            If strResult(15).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 8) = "Âm tính"
            Else
                Me.vResult(1, 8) = strResult(15).Substring(5, 7).Trim
            End If

            'ERY
            Me.vResult(0, 9) = strResult(16).Substring(2, 3).Trim
            If strResult(16).Substring(5, 7).Trim = "neg" Then
                Me.vResult(1, 9) = "Âm tính"
            Else
                Me.vResult(1, 9) = strResult(16).Substring(5, 7).Trim
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)

            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If

            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            ' Ket thuc cap nhat CSDL

        Catch ex As Exception
            f.AppendAllText("C:\LOG\Urisys1100_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
            Return False
        End Try
        Return True
    End Function

    Public Function spHitachi717(ByVal strInput As String, _
                                      ByVal pTestTypeID As Int16, _
                                      ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim iCount, iPara, iTest As Int16
        Dim pTestDate, pTestSeq, pBarcode, strbackup As String
        Dim iAccount As Integer
        Dim iError As Integer = 0
        Dim cmd As New SqlCommand

        Try

            'strInput = f.ReadAllText("C:\LOG\hitachi.txt")
            strbackup = strInput
            'f.AppendAllText("C:\LOG\hitachi_strbackup.txt", Now.ToString & "-" & strbackup & vbCrLf)
            iTest = 0
            pBarcode = strInput.Substring(9, 11).Trim
            If pBarcode = "" Then
                Return True
            End If
            Dim S2 As String
            strInput = strInput.Substring(21)
            iCount = 0
            ReDim vResult(5, 0)
            'Try
            Do
                If iCount > 0 Then
                    ReDim Preserve vResult(5, iCount)
                End If
                S2 = strInput.Substring(0, 9)
                iError = iError + 1
                vResult(0, iCount) = S2.Substring(0, 2).Trim
                vResult(1, iCount) = S2.Substring(2, 6).Trim 'Thừa dấu ấm nếu chọn cả 6 ký tự
                'vResult(1, iCount) = S2.Substring(3, 5).Trim
                iCount += 1
                strInput = strInput.Substring(9)
            Loop While (strInput.Length > 9)
            'Catch ex As Exception
            '    f.AppendAllText("C:\LOG\hitachi_iError.txt", strbackup & vbCrLf & iError.ToString & vbCrLf & ex.ToString & vbCrLf & strInput & vbCrLf)
            '    Exit Function
            'End Try
            'pTestDate = String.Format(Now.Date, "DD/MM/YY")
            pTestDate = Format(Date.Today.Day, "0#") & "/" & Format(Date.Today.Month, "0#") & "/" & Date.Today.Year.ToString
            pBarcode = Date.Today.Year.ToString.Substring(2) & Format(Date.Today.Month, "0#") & Format(Date.Today.Day, "0#") & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)

            Dim Test_ID, Patient_ID As Int32
            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)

            Try
                ' Xu ly tham so them
                iAccount = Get_Account_Hitachi717()
                Select Case iAccount
                    Case "1" 'Hai Phong
                        Dim iHDLC, iTRIG, iChol, iALB, iProT, iBILT, iBILD, iCA As Double

                        iHDLC = -1
                        iTRIG = -1
                        iChol = -1
                        iALB = -1
                        iProT = -1
                        iBILT = -1
                        iBILD = -1
                        iCA = -1

                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            ' Bil - T
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 6 Then
                                iTest = 1
                                iBILT = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' Bil - D
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 7 Then
                                iTest = 2
                                iBILD = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' Pro - T
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 3 Then
                                iTest = 3
                                iProT = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' AlB
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 4 Then
                                iTest = 4
                                iALB = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' chol
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 10 Then
                                iTest = 5
                                iChol = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' Trig
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 11 Then
                                iTest = 6
                                iTRIG = Convert.ToDouble(Me.vResult(1, iCount).Trim)

                            End If

                            ' HDLC
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 12 Then
                                iTest = 7
                                iHDLC = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If

                            ' Ca
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 16 Then
                                iTest = 8
                                iCA = Convert.ToDouble(Me.vResult(1, iCount).Trim)
                            End If
                        Next

                        ' BILG
                        If iBILT > 0 And iBILD > 0 Then
                            iTest = 9
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "47"
                            vResult(1, iCount) = Str(iBILT - iBILD)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' GLO
                        If iProT > 0 And iALB > 0 Then
                            iTest = 10
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "48"
                            vResult(1, iCount) = Str(iProT - iALB)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' A/G
                        If iProT > 0 And iALB > 0 Then
                            iTest = 11
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "49"
                            vResult(1, iCount) = Str(iALB / (iProT - iALB))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' LDL
                        If iChol > 0 And iHDLC > 0 And iTRIG > 0 Then
                            iTest = 12
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "50"
                            vResult(1, iCount) = Str(iChol - (iTRIG / 2.2 + iHDLC))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' CA - I
                        If iProT > 0 And iCA > 0 Then
                            iTest = 13
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "51"
                            vResult(1, iCount) = Str((65 * iCA + 11.4) / (iProT + 66) - 0.16)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If
                        Exit Select

                    Case "2" 'NHTD
                        Dim iHDLC, iTRIG, iChol, iALB, iProT, iBILT, iBILD, iCA As Double

                        iHDLC = -1
                        iTRIG = -1
                        iChol = -1
                        iALB = -1
                        iProT = -1
                        iBILT = -1
                        iBILD = -1
                        iCA = -1

                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            ' Bil - Toan phan
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 10 Then
                                iTest = 1
                                iBILT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Bil - Truc tiep
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 11 Then
                                iTest = 2
                                iBILD = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Pro - T
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 4 Then
                                iTest = 4
                                iProT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            'AlB
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 5 Then
                                iTest = 5
                                iALB = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            '' chol
                            'If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 2 Then
                            '    iTest = 5
                            '    iChol = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            'End If

                            ' Trig
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 3 Then
                                iTest = 6
                                iTRIG = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' HDLC
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 16 Then
                                iTest = 7
                                iHDLC = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If
                        Next

                        ' A/G
                        If iProT > 0 And iALB > 0 Then
                            iTest = 11
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "49"
                            vResult(1, iCount) = Str(iALB / (iProT - iALB))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' LDL-C
                        If iChol > 0 And iHDLC > 0 And iTRIG > 0 Then
                            iTest = 12
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "48"
                            vResult(1, iCount) = Str(iChol - (iTRIG / 2.2 + iHDLC))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        'Globulin
                        If iProT > 0 And iALB > 0 Then
                            iTest = 12
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "50"
                            vResult(1, iCount) = Str(iProT - iALB)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        'BIL - GT
                        If iProT > 0 And iALB > 0 Then
                            iTest = 13
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "51"
                            vResult(1, iCount) = Str(iBILT - iBILD)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If
                        Exit Select

                    Case "3" 'Xay Dung
                        Dim iHDLC, iTRIG, iChol, iALB, iProT, iBILT, iBILD, iCA As Double

                        iHDLC = -1
                        iTRIG = -1
                        iChol = -1
                        iALB = -1
                        iProT = -1
                        iBILT = -1
                        iBILD = -1
                        iCA = -1

                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            ' Bil - T - Total - Bil Toan phan
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 6 Then
                                iTest = 1
                                iBILT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Bil - D - Direct - Bil Truc tiep
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 7 Then
                                iTest = 2
                                iBILD = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Pro - T
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 3 Then
                                iTest = 3
                                iProT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' AlB
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 4 Then
                                iTest = 4
                                iALB = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' chol
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 10 Then
                                iTest = 5
                                iChol = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Trig
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 11 Then
                                iTest = 6
                                iTRIG = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' HDLC
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 12 Then
                                iTest = 7
                                iHDLC = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                        Next

                        ' BIL - G - Bil gian tiep
                        If iBILT > 0 And iBILD > 0 Then
                            iTest = 9
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "48"
                            vResult(1, iCount) = Str(iBILT - iBILD)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' GLO or GLOB
                        If iProT > 0 And iALB > 0 Then
                            iTest = 10
                            iCount = vResult.GetLength(1)

                            ReDim Preserve vResult(5, iCount)

                            vResult(0, iCount) = "49"
                            vResult(1, iCount) = Str(iProT - iALB)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' A/G
                        If iProT > 0 And iALB > 0 Then
                            iTest = 11
                            iCount = vResult.GetLength(1)

                            ReDim Preserve vResult(5, iCount)

                            vResult(0, iCount) = "50"
                            vResult(1, iCount) = Str(iALB / (iProT - iALB))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' LDL or LDL - C
                        If iChol > 0 And iHDLC > 0 And iTRIG > 0 Then
                            iTest = 12
                            iCount = vResult.GetLength(1)

                            ReDim Preserve vResult(5, iCount)

                            vResult(0, iCount) = "47"
                            vResult(1, iCount) = Str(iChol - (iTRIG / 2.2 + iHDLC))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        Exit Select

                    Case "4" '1C Việt Đức
                        Dim iHDLC, iTRIG, iChol, iALB, iProT, iBILT, iBILD, iCA As Double

                        iHDLC = -1
                        iTRIG = -1
                        iChol = -1
                        iALB = -1
                        iProT = -1
                        iBILT = -1
                        iBILD = -1
                        iCA = -1

                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            ' Bil - T - Total - Bil Toan phan
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 10 Then
                                iTest = 1
                                iBILT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Bil - D - Direct - Bil Truc tiep
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 11 Then
                                iTest = 2
                                iBILD = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Pro - T
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 12 Then
                                iTest = 3
                                iProT = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' AlB
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 15 Then
                                iTest = 4
                                iALB = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' chol
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 17 Then
                                iTest = 5
                                iChol = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' Trig
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 16 Then
                                iTest = 6
                                iTRIG = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                            ' HDLC
                            If Convert.ToInt16(Me.vResult(0, iCount).Trim) = 18 Then
                                iTest = 7
                                iHDLC = Convert.ToDouble(Trim_All(Me.vResult(1, iCount).Trim))
                            End If

                        Next

                        ' BIL - G - Bil gian tiep
                        If iBILT > 0 And iBILD > 0 Then
                            iTest = 9
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "47"
                            vResult(1, iCount) = Str(iBILT - iBILD)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' GLO or GLOB
                        If iProT > 0 And iALB > 0 Then
                            iTest = 10
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "51"
                            vResult(1, iCount) = Str(iProT - iALB)
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' A/G
                        If iProT > 0 And iALB > 0 Then
                            iTest = 11
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "49"
                            vResult(1, iCount) = Str(iALB / (iProT - iALB))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        ' LDL or LDL - C
                        If iChol > 0 And iHDLC > 0 And iTRIG > 0 Then
                            iTest = 12
                            iCount = vResult.GetLength(1)
                            ReDim Preserve vResult(5, iCount)
                            vResult(0, iCount) = "50"
                            vResult(1, iCount) = Str(iChol - (iTRIG / 2.2 + iHDLC))
                            Me.vResult(2, iCount) = ""
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        End If

                        Exit Select

                End Select
            Catch ex As Exception
                f.AppendAllText("C:\LOG\hitachi_ErrorPlus.txt", Now.ToString & vbCrLf & strbackup & vbCrLf & iTest.ToString & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
            End Try

            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    'vResult(1, iCount) = strResult
                    Me.vResult(2, iCount) = ""
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount) Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(1, iCount) = Me.spGetDataToView(Me.vResult(1, iCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next

            End If
            Dim pTestDetailID As Int64
            Dim iCheck As Integer = 0
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                Try
                    iCheck = spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID)
                    If iCheck Then
                        ' Neu da co du lieu nay
                        Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                    Else
                        Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                    End If
                Catch ex As Exception
                    f.AppendAllText("C:\LOG\hitachi_iCheck_Error.txt", Now.ToString & vbCrLf & pBarcode & vbCrLf & Me.vResult(0, iCount) & vbCrLf & pTestDetailID.ToString & vbCrLf & vbCrLf)
                End Try
            Next
            f.AppendAllText("C:\LOG\hitachi_Input.txt", Now.ToString & "-" & strbackup & vbCrLf)
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\hitachi_Exception.txt", Now.ToString & "-" & strbackup & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try
    End Function

    Public Function spRatech(ByVal strInput As String, _
                             ByVal pTestTypeID As Int16, _
                             ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pMonth As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16

        'strInput = f.ReadAllText("C:\a.txt")
        f.AppendAllText("C:\LOG\744.txt", strInput)
        Try
            strALL = Split(strInput, "PATIENT'S NAME ")
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                If InStr(strALL(iPatientNumber).ToUpper, "NA") >= 0 And InStr(strALL(iPatientNumber), "K") > 0 And InStr(strALL(iPatientNumber).ToUpper, "CL") >= 0 And InStr(strALL(iPatientNumber).ToUpper, "MMOL/L") >= 0 Then
                    ' Neu co du lieu thi moi xet

                    strTemp = Split(strALL(iPatientNumber), Chr(13))
                    iCount = 0

                    Do While iCount < strTemp.GetLength(0)
                        If strTemp(iCount) <> vbCrLf And strTemp(iCount).Trim <> "" Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If
                            strResult(strResult.GetLength(0) - 1) = strTemp(iCount)
                        End If
                        iCount += 1
                    Loop

                    If strResult.Length > 5 Then
                        strResult(0) = strResult(1)
                        strResult(1) = strResult(2)
                        strResult(2) = strResult(3)
                        strResult(3) = strResult(4)
                        strResult(4) = strResult(5)
                    End If

                    pBarcode = strResult(0).Substring(strResult(0).IndexOf("#") + 1)
                    pTestDate = strResult(4).Substring(0, strResult(4).IndexOf(";"))

                    pMonth = "00"
                    Select Case pTestDate.Substring(0, 3)
                        Case "JAN"
                            pMonth = "01"
                        Case "FEB"
                            pMonth = "02"
                        Case "MAR"
                            pMonth = "03"
                        Case "APR"
                            pMonth = "04"
                        Case "MAY"
                            pMonth = "05"
                        Case "JUN"
                            pMonth = "06"
                        Case "JUL"
                            pMonth = "07"
                        Case "AUG"
                            pMonth = "08"
                        Case "SEP"
                            pMonth = "09"
                        Case "OCT"
                            pMonth = "10"
                        Case "NOV"
                            pMonth = "11"
                        Case "DEC"
                            pMonth = "12"
                    End Select
                    pTestDate = pTestDate.Substring(4, 2) & "/" & pMonth & "/" & "20" & pTestDate.Substring(7, 2)

                    pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = pBarcode.Substring(2)


                    ' Lay gia tri cho mang KQ. May nay chi co 3 thong so
                    ReDim Me.vResult(5, 2)

                    Me.vResult(0, 0) = "NA"
                    strTemp = strResult(1).Trim.Substring(3).Trim.Split(" ")
                    Me.vResult(1, 0) = strTemp(0).Trim
                    Me.vResult(2, 0) = strTemp(1).Trim

                    Me.vResult(0, 1) = "K"
                    strTemp = strResult(2).Trim.Substring(3).Trim.Split(" ")
                    Me.vResult(1, 1) = strTemp(0).Trim
                    Me.vResult(2, 1) = strTemp(1).Trim


                    Me.vResult(0, 2) = "CL"
                    strTemp = strResult(3).Trim.Substring(3).Trim.Split(" ")
                    Me.vResult(1, 2) = strTemp(0).Trim
                    Me.vResult(2, 2) = strTemp(1).Trim

                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)

                    If Me.tblParaName Is Nothing Then
                        For iCount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        Next
                    Else
                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                                    Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For iCount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL

                End If
            Next

            Return True
        Catch ex As Exception
            f.WriteAllText("C:\LOG\ratech.txt", strInput)
            Return False
        End Try
    End Function

    Public Function sp248(ByVal strInput As String, _
                                     ByVal pTestTypeID As Int16, _
                                     ByVal PortNum As Int16) As Boolean
        Dim f As File
        Dim pTestDate, pBarcode, pMonth As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16
        f.AppendAllText("C:\LOG\248Input.txt", strInput & vbCrLf)
        'strInput = f.ReadAllText("C:\LOG\248.txt")
        Try
            strALL = Split(strInput, vbCrLf)
            pTestDate = strALL(2)
            pMonth = "00"
            Select Case pTestDate.Substring(21, 3).ToUpper
                Case "JAN"
                    pMonth = "01"
                Case "FEB"
                    pMonth = "02"
                Case "MAR"
                    pMonth = "03"
                Case "APR"
                    pMonth = "04"
                Case "MAY"
                    pMonth = "05"
                Case "JUN"
                    pMonth = "06"
                Case "JUL"
                    pMonth = "07"
                Case "AUG"
                    pMonth = "08"
                Case "SEP"
                    pMonth = "09"
                Case "OCT"
                    pMonth = "10"
                Case "NOV"
                    pMonth = "11"
                Case "DEC"
                    pMonth = "12"
            End Select
            If pTestDate.Substring(25, 1) = " " Then
                pTestDate = "0" & pTestDate.Substring(26, 1) & "/" & pMonth & "/" & pTestDate.Substring(28, 4)
            Else
                pTestDate = pTestDate.Substring(25, 2) & "/" & pMonth & "/" & pTestDate.Substring(28, 4)
            End If
            'Co ID thi moi xet
            pBarcode = strALL(5)
            pBarcode = pBarcode.Substring(11, pBarcode.Length - 11)
            If pBarcode.Trim <> "" Then
                pBarcode = pBarcode.Trim
            Else
                f.AppendAllText("C:\LOG\248Exception.txt", strInput & vbCrLf & "Khong co ID" & vbCrLf)
                Return False
            End If
            pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            pBarcode = pBarcode.Substring(2)

            ' Lay gia tri cho mang KQ. May nay co 8 thong so
            ReDim Me.vResult(5, 7)

            Me.vResult(0, 0) = "pH"
            Me.vResult(1, 0) = strALL(8).Substring(14, 5).Trim

            Me.vResult(0, 1) = "pCO2"
            Me.vResult(1, 1) = strALL(9).Substring(14, 5).Trim

            Me.vResult(0, 2) = "pO2"
            Me.vResult(1, 2) = strALL(10).Substring(14, 5).Trim

            Me.vResult(0, 3) = "HCO3act"
            Me.vResult(1, 3) = strALL(19).Substring(14, 5).Trim

            Me.vResult(0, 4) = "HCO3std"
            Me.vResult(1, 4) = strALL(20).Substring(14, 5).Trim

            Me.vResult(0, 5) = "BE(ecf)"
            Me.vResult(1, 5) = strALL(21).Substring(14, 5).Trim

            Me.vResult(0, 6) = "BE(B)"
            Me.vResult(1, 6) = strALL(22).Substring(14, 5).Trim

            Me.vResult(0, 7) = "O2SAT"
            Me.vResult(1, 7) = strALL(23).Substring(14, 5).Trim

            Me.spGetID(pBarcode, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)

            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If

            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            ' Ket thuc cap nhat CSDL
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\248Exception.txt", strInput & vbCrLf & ex.ToString & vbCrLf)
        End Try
    End Function

    Public Function spIlyte(ByVal strInput As String, _
                                 ByVal pTestTypeID As Int16, _
                                 ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pMonth As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16

        'strInput = f.ReadAllText("C:\LOG\IlyteBarcode.txt")

        Try
            strALL = Split(strInput, "PATIENT'S NAME  ")
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                If InStr(strALL(iPatientNumber).ToUpper, "NA") >= 0 And InStr(strALL(iPatientNumber), "K") > 0 And InStr(strALL(iPatientNumber).ToUpper, "CL") >= 0 And InStr(strALL(iPatientNumber).ToUpper, "MMOL/L") >= 0 Then
                    ' Neu co du lieu thi moi xet
                    strTemp = Split(strALL(iPatientNumber), Chr(13) & Chr(10))
                    iCount = 0

                    Do While iCount < strTemp.GetLength(0)
                        If strTemp(iCount) <> vbCrLf And strTemp(iCount).Trim <> "" Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If
                            strResult(strResult.GetLength(0) - 1) = strTemp(iCount)
                        End If
                        iCount += 1
                    Loop
                    If strResult.Length = 5 Then
                        strResult(2) = strResult(4)
                    End If

                    'Co ID thi moi xet, con khong thi gan cho ID = 0
                    If InStr(strResult(0), "ID#") Then
                        pBarcode = strResult(0).Substring(strResult(0).IndexOf("#") + 2).Trim
                    Else
                        pBarcode = "0000"
                        'f.AppendAllText("C:\LOG\IlyteNoID.txt", strInput & vbCrLf & "Khong co ID" & vbCrLf)
                        'Return False
                    End If
                    'pBarcode = "100908070001"

                    pTestDate = strResult(3)
                    pMonth = "00"
                    Select Case pTestDate.Substring(0, 3)
                        Case "JAN"
                            pMonth = "01"
                        Case "FEB"
                            pMonth = "02"
                        Case "MAR"
                            pMonth = "03"
                        Case "APR"
                            pMonth = "04"
                        Case "MAY"
                            pMonth = "05"
                        Case "JUN"
                            pMonth = "06"
                        Case "JUL"
                            pMonth = "07"
                        Case "AUG"
                            pMonth = "08"
                        Case "SEP"
                            pMonth = "09"
                        Case "OCT"
                            pMonth = "10"
                        Case "NOV"
                            pMonth = "11"
                        Case "DEC"
                            pMonth = "12"
                    End Select
                    pTestDate = pTestDate.Substring(4, 2) & "/" & pMonth & "/" & "20" & pTestDate.Substring(7, 2)
                    pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = pBarcode.Substring(2)

                    ' Lay gia tri cho mang KQ. May nay chi co 3 thong so
                    ReDim Me.vResult(5, 2)

                    Me.vResult(0, 0) = "NA"
                    Me.vResult(1, 0) = strResult(1).Trim.Substring(3, 5).Trim

                    Me.vResult(0, 1) = "K"
                    Me.vResult(1, 1) = strResult(1).Trim.Substring(11, 5).Trim

                    Me.vResult(0, 2) = "CL"
                    Me.vResult(1, 2) = strResult(1).Trim.Substring(20, 5).Trim

                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)

                    If Me.tblParaName Is Nothing Then
                        For iCount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        Next
                    Else
                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                                    Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For iCount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL
                End If
            Next
            f.AppendAllText("C:\LOG\IlyteInput.txt", Now.ToString & vbCrLf & strInput)
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\IlyteException.txt", Now.ToString & vbCrLf & ex.ToString & vbCrLf & strInput)
            Return False
        End Try
    End Function

    Public Function spACL100(ByVal strInput As String, _
                                     ByVal pTestTypeID As Int16, _
                                     ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pYear, pMonth, pDay As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16
        Try
            'strInput = f.ReadAllText("C:\LOG\ACL.txt")

            File.AppendAllText("C:\LOG\ACL_Input.txt", strInput)
            File.AppendAllText("C:\LOG\ACL_Input.txt", Now.ToString & vbCrLf & "-------------------------------------------------" & vbCrLf)
            strResult = Split(strInput, vbCrLf & vbCrLf)

            pTestDate = strResult(0).Substring(9, 4).Trim
            pMonth = "00"
            Select Case pTestDate
                Case "JAN"
                    pMonth = "01"
                Case "FEB"
                    pMonth = "02"
                Case "MAR"
                    pMonth = "03"
                Case "APR"
                    pMonth = "04"
                Case "MAY"
                    pMonth = "05"
                Case "JUN"
                    pMonth = "06"
                Case "JUL"
                    pMonth = "07"
                Case "AUG"
                    pMonth = "08"
                Case "SEP"
                    pMonth = "09"
                Case "OCT"
                    pMonth = "10"
                Case "NOV"
                    pMonth = "11"
                Case "DEC"
                    pMonth = "12"
            End Select
            pDay = strResult(0).Substring(6, 2).Trim
            If pDay.Length = 1 Then
                pDay = "0" & pDay
            End If
            pYear = strResult(0).Substring(14, 3).Trim
            pTestDate = pDay & "/" & pMonth & "/" & "20" & pYear

            'Tùy từng Test ID mà xử lý
            If strResult(0).Substring(0, 2) = "1B" Then
                'Lay gia tri cho mang KQ. May nay chi co 6 thong so
                ReDim Me.vResult(5, 5)

                Me.vResult(0, 0) = "PT"
                Me.vResult(1, 0) = strResult(3).Substring(0, 5).Trim

                Me.vResult(0, 1) = "PT(%)"
                Me.vResult(1, 1) = strResult(3).Trim.Substring(5, 3).Trim

                Me.vResult(0, 2) = "PT-INR"
                Me.vResult(1, 2) = strResult(3).Trim.Substring(8, 5).Trim

                Me.vResult(0, 3) = "FIB"
                Me.vResult(1, 3) = strResult(3).Trim.Substring(13, 5).Trim

                Me.vResult(0, 4) = "APTT"
                Me.vResult(1, 4) = strResult(4).Trim.Substring(0, 4).Trim

                Me.vResult(0, 5) = "APTT-INR"
                Me.vResult(1, 5) = strResult(4).Trim.Substring(4, 4).Trim

                pBarcode = strResult(5).Substring(0, 12).Trim

            Else
                File.AppendAllText("C:\LOG\ACL_Input.txt", "Trường hợp trên chưa được xử lý. Code thêm đê.")
                Return False
            End If

            pBarcode = pYear & pMonth & pDay & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)
            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If
            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next

            f.AppendAllText("C:\LOG\ACL100_Success.txt", Now.ToString & vbCrLf & strInput)
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\ACL100_Exception.txt", Now.ToString & vbCrLf & ex.ToString & vbCrLf & strInput)
            Return False
        End Try
    End Function

    Public Function spEasylyte(ByVal strInput As String, _
                                 ByVal pTestTypeID As Int16, _
                                 ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pMonth As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16

        'strInput = f.ReadAllText("C:\LOG\IlyteBarcode.txt")

        Try
            strALL = Split(strInput, "PATIENT'S NAME  ")
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                If InStr(strALL(iPatientNumber).ToUpper, "NA") >= 0 And InStr(strALL(iPatientNumber), "K") > 0 And InStr(strALL(iPatientNumber).ToUpper, "CL") >= 0 And InStr(strALL(iPatientNumber).ToUpper, "MMOL/L") >= 0 Then
                    ' Neu co du lieu thi moi xet
                    strTemp = Split(strALL(iPatientNumber), Chr(13) & Chr(10))
                    iCount = 0

                    Do While iCount < strTemp.GetLength(0)
                        If strTemp(iCount) <> vbCrLf And strTemp(iCount).Trim <> "" Then
                            If strResult Is Nothing Then
                                ReDim strResult(0)
                            Else
                                ReDim Preserve strResult(strResult.GetLength(0))
                            End If
                            strResult(strResult.GetLength(0) - 1) = strTemp(iCount)
                        End If
                        iCount += 1
                    Loop
                    'If strResult.Length = 5 Then
                    '    strResult(2) = strResult(4)
                    'End If

                    'Co ID thi moi xet, con khong thi gan cho ID = 0
                    If InStr(strResult(0), "ID#") Then
                        pBarcode = strResult(0).Substring(strResult(0).IndexOf("#") + 2).Trim
                    Else
                        pBarcode = "0000"
                        'f.AppendAllText("C:\LOG\IlyteNoID.txt", strInput & vbCrLf & "Khong co ID" & vbCrLf)
                        'Return False
                    End If
                    'pBarcode = "100908070001"

                    pTestDate = strResult(strResult.GetLength(0) - 1)
                    pMonth = "00"
                    Select Case pTestDate.Substring(0, 3)
                        Case "JAN"
                            pMonth = "01"
                        Case "FEB"
                            pMonth = "02"
                        Case "MAR"
                            pMonth = "03"
                        Case "APR"
                            pMonth = "04"
                        Case "MAY"
                            pMonth = "05"
                        Case "JUN"
                            pMonth = "06"
                        Case "JUL"
                            pMonth = "07"
                        Case "AUG"
                            pMonth = "08"
                        Case "SEP"
                            pMonth = "09"
                        Case "OCT"
                            pMonth = "10"
                        Case "NOV"
                            pMonth = "11"
                        Case "DEC"
                            pMonth = "12"
                    End Select
                    pTestDate = pTestDate.Substring(4, 2) & "/" & pMonth & "/" & "20" & pTestDate.Substring(7, 2)
                    pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = pBarcode.Substring(2)

                    ' Lay gia tri cho mang KQ. May nay chi co 3 thong so
                    ReDim Me.vResult(5, 2)

                    Me.vResult(0, 0) = "NA"
                    Me.vResult(1, 0) = strResult(1).Trim.Substring(3, 5).Trim

                    Me.vResult(0, 1) = "K"
                    Me.vResult(1, 1) = strResult(1).Trim.Substring(11, 5).Trim

                    Me.vResult(0, 2) = "CL"
                    Me.vResult(1, 2) = strResult(1).Trim.Substring(20, 5).Trim

                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                    ' Lay thong tin thong so
                    tblParaName = Me.spGetParaInfor(PortNum)

                    If Me.tblParaName Is Nothing Then
                        For iCount = 0 To Me.vResult.GetLength(0) - 1
                            Me.vResult(3, iCount) = ""
                            Me.vResult(4, iCount) = ""
                            Me.vResult(5, iCount) = 0
                        Next
                    Else
                        For iCount = 0 To Me.vResult.GetLength(1) - 1
                            For iPara = 0 To Me.tblParaName.Rows.Count - 1
                                If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                                    Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                                    Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                                    Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                                    Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                                    Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                                End If
                            Next
                        Next
                        ' Ket thuc cap nhat thong tin cau hinh
                    End If

                    ' Cap nhat vao CSDL
                    For iCount = 0 To Me.vResult.GetLength(1) - 1
                        If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                        End If
                    Next
                    ' Ket thuc cap nhat CSDL
                End If
            Next
            f.AppendAllText("C:\LOG\EasylyteInput.txt", Now.ToString & vbCrLf & strInput)
            Return True
        Catch ex As Exception
            f.AppendAllText("C:\LOG\EasylyteException.txt", Now.ToString & vbCrLf & ex.ToString & vbCrLf & strInput)
            Return False
        End Try
    End Function

    Public Function spRoche9180(ByVal strInput As String, _
                                     ByVal pTestTypeID As Int16, _
                                     ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pMonth As String
        Dim strALL, strResult, strTemp As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iCount, iPara As Int16

        'strInput = f.ReadAllText("C:\LOG\9180DienGiai.txt")
        f.WriteAllText("C:\LOG\9180Log.txt", strInput & vbCrLf)
        Try
            strResult = Split(strInput, vbCrLf)
            pMonth = "00"
            Select Case strResult(3).Substring(3, 3)
                Case "JAN"
                    pMonth = "01"
                Case "FEB"
                    pMonth = "02"
                Case "MAR"
                    pMonth = "03"
                Case "APR"
                    pMonth = "04"
                Case "MAY"
                    pMonth = "05"
                Case "JUN"
                    pMonth = "06"
                Case "JUL"
                    pMonth = "07"
                Case "AUG"
                    pMonth = "08"
                Case "SEP"
                    pMonth = "09"
                Case "OCT"
                    pMonth = "10"
                Case "NOV"
                    pMonth = "11"
                Case "DEC"
                    pMonth = "12"
            End Select
            pTestDate = strResult(3).Substring(0, 3).Trim & "/" & pMonth & "/" & "20" & strResult(3).Substring(6, 2).Trim
            pBarcode = strResult(10).Substring(10).Trim

            ' Lay gia tri cho mang KQ. May nay chi co 3 thong so
            ReDim Me.vResult(5, 2)

            Me.vResult(0, 0) = strResult(13).Substring(0, 2).Trim
            Me.vResult(1, 0) = strResult(13).Substring(3, 5).Trim

            Me.vResult(0, 1) = strResult(14).Substring(0, 2).Trim
            Me.vResult(1, 1) = strResult(14).Substring(3, 5).Trim

            Me.vResult(0, 2) = strResult(15).Substring(0, 2).Trim
            Me.vResult(1, 2) = strResult(15).Substring(3, 5).Trim

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Lay thong tin thong so
            tblParaName = Me.spGetParaInfor(PortNum)

            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(0) - 1
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name").ToString.ToUpper = Me.vResult(0, iCount).Trim.ToUpper Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
                ' Ket thuc cap nhat thong tin cau hinh
            End If

            ' Cap nhat vao CSDL
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            ' Ket thuc cap nhat CSDL
            Return True

        Catch ex As Exception
            f.WriteAllText("C:\LOG\9180Exception.txt", strInput & vbCrLf & ex.ToString & vbCrLf)
            Return False
        End Try
    End Function

    Public Function spAutoCounter(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean

        Dim f As File
        Dim pTestDate, pBarcode, pTestSEQ As String
        Dim strALL, strResult As String()
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iPatientNumber, iParaPos, iPos, iStart As Int16

        'strInput = f.ReadAllText("C:\LOG\count.txt")

        Try

            strALL = Split(strInput, "#####")
            For iPatientNumber = 0 To strALL.GetLength(0) - 1
                strResult = Split(strALL(iPatientNumber), vbCrLf)

                iStart = -1
                For iPos = 0 To strResult.GetLength(0) - 1
                    If strResult(iPos).Trim = "+++++" Then
                        iStart = iPos
                        Exit For
                    End If
                Next
                If iStart < 0 Then
                    Exit For
                End If

                pTestDate = strResult(iStart + 2).Substring(5).Trim
                pTestDate = pTestDate.Substring(0, 10)
                pBarcode = strResult(iStart + 5).Substring(3).Trim
                pTestSEQ = strResult(iStart + 6).Substring(4).Trim
                pBarcode = pTestDate & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)



                iPos = 0
                iStart = iStart + 7
                Do While iStart < strResult.GetLength(0) - 1
                    iParaPos = strResult(iStart).IndexOf("=")
                    If iParaPos >= 0 Then
                        ReDim Preserve Me.vResult(5, iPos)
                        Me.vResult(0, iPos) = strResult(iStart).Substring(0, iParaPos)
                        strResult(iStart) = strResult(iStart).Substring(iParaPos + 1).Trim
                        Me.vResult(1, iPos) = strResult(iStart).Substring(0, strResult(iStart).IndexOf(" "))

                        iPos = iPos + 1
                        iStart += 1
                    End If
                Loop

                ' Lay du lieu dinh dang
                tblParaName = Me.spGetParaInfor(PortNum)
                If Me.tblParaName Is Nothing Then
                    For iPos = 0 To Me.vResult.GetLength(1) - 1
                        Me.vResult(2, iPos) = ""
                        Me.vResult(3, iPos) = ""
                        Me.vResult(4, iPos) = ""
                        Me.vResult(5, iPos) = 0
                    Next
                Else
                    ' Co du lieu dinh dang thi moi xet
                    For iPos = 0 To Me.vResult.GetLength(1) - 1
                        For iParaPos = 0 To Me.tblParaName.Rows.Count - 1
                            If Me.tblParaName.Rows(iParaPos).Item("Alias_name") = Me.vResult(0, iPos) Then
                                Me.vResult(0, iPos) = Me.tblParaName.Rows(iParaPos).Item("Data_name")
                                Me.vResult(1, iPos) = Me.spGetDataToView(Me.vResult(1, iPos), Me.tblParaName.Rows(iParaPos).Item("Data_Point"))
                                Me.vResult(2, iPos) = Me.tblParaName.Rows(iParaPos).Item("Measure_Unit")
                                Me.vResult(3, iPos) = Me.tblParaName.Rows(iParaPos).Item("Normal_Level")
                                Me.vResult(4, iPos) = Me.tblParaName.Rows(iParaPos).Item("Normal_LevelW")
                                Me.vResult(5, iPos) = Me.tblParaName.Rows(iParaPos).Item("Data_Sequence")
                            End If
                        Next
                    Next
                End If

                ' Cap nhat vao CSDL 
                Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
                For iPos = 0 To Me.vResult.GetLength(1) - 1
                    If spCheckBarcode(pBarcode, Me.vResult(0, iPos), pTestDetailID) Then
                        ' Neu da co du lieu nay
                        Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iPos))
                    Else
                        Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSEQ, pTestDate, Me.vResult(5, iPos), pBarcode, Me.vResult(0, iPos), Me.vResult(1, iPos), Me.vResult(2, iPos), Me.vResult(3, iPos), Me.vResult(4, iPos))
                    End If
                Next

                ' Xu ly xong 1 mau
            Next

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function spAdvia60(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean
        Try
            strInput = strInput.Substring(strInput.IndexOf(Chr(2)))
            'strInput = IO.File.ReadAllText("G:\VietBaIT\Document\Ket noi cac may\Advia60\bn 27-5-1009.txt")

            'IO.File.WriteAllText("C:\LOG\Advia60" & "_" & Now.Year.ToString & Now.Month.ToString & Now.Day & "_" & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & Now.Millisecond & ".txt", strInput)
            Dim strArray() As String
            Dim str1() As String

            str1 = Split(strInput, strInput.Chars(0))
            strArray = Split(str1(1), str1(1).Chars(5))


            If strArray.Length < 20 Then
                Exit Function
            End If

            'Dim strResult(1, 16) As String
            Dim iCount, iPara, iQC As Int16
            Dim Test_ID, Patient_ID As Int64
            Dim pTestDate, pTestSeq, pBarcode As String
            Dim cmd As New SqlCommand


            'Dim f As File
            'f.WriteAllText("C:\LOG\a.txt", strInput)
            pTestDate = strArray(3).Substring(2, 8).Trim
            pTestSeq = strArray(0)
            pTestDate = pTestDate.Substring(0, 2) & "/" & pTestDate.Substring(3, 2) & "/" & "20" & pTestDate.Substring(6, 2)

            pBarcode = Me.spGetBarcode(strArray(6).Substring(2).Trim)
            If pBarcode = "0000" Then
                Exit Function
            End If
            pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode

            If Me.spCheckSequence(pTestSeq, pTestDate) Then
                Exit Function
            End If


            tblParaName = Me.spGetParaInfor(PortNum)
            ReDim Me.vResult(5, 17)

            iQC = 0 ' Dung de kiem tra khi khoi dong

            Me.vResult(0, 0) = "WBC"
            Me.vResult(1, 0) = strArray(9).Substring(2, 5).Trim

            Me.vResult(0, 1) = "RBC"
            Me.vResult(1, 1) = strArray(10).Substring(2, 5).Trim

            Me.vResult(0, 2) = "HGB"
            Me.vResult(1, 2) = strArray(11).Substring(2, 5).Trim

            Me.vResult(0, 3) = "HCT"
            Me.vResult(1, 3) = strArray(12).Substring(2, 5).Trim

            Me.vResult(0, 4) = "MCV"
            Me.vResult(1, 4) = strArray(13).Substring(2, 5).Trim

            Me.vResult(0, 5) = "MCH"
            Me.vResult(1, 5) = strArray(14).Substring(2, 5).Trim

            Me.vResult(0, 6) = "MCHC"
            Me.vResult(1, 6) = strArray(15).Substring(2, 5).Trim

            Me.vResult(0, 7) = "RDW"
            Me.vResult(1, 7) = strArray(16).Substring(2, 5).Trim

            Me.vResult(0, 8) = "PLT"
            Me.vResult(1, 8) = strArray(17).Substring(2, 5).Trim

            Me.vResult(0, 9) = "MPV"
            Me.vResult(1, 9) = strArray(18).Substring(2, 5).Trim

            Me.vResult(0, 10) = "PCT"
            Me.vResult(1, 10) = strArray(19).Substring(2, 5).Trim

            Me.vResult(0, 11) = "PDW"
            Me.vResult(1, 11) = strArray(20).Substring(2, 5).Trim

            Me.vResult(0, 12) = "%LYM"
            Me.vResult(1, 12) = strArray(21).Substring(2, 5).Trim

            Me.vResult(0, 13) = "%MON"
            Me.vResult(1, 13) = strArray(22).Substring(2, 5).Trim

            Me.vResult(0, 14) = "%GRA"
            Me.vResult(1, 14) = strArray(23).Substring(2, 5).Trim

            Me.vResult(0, 15) = "#LYM"
            Me.vResult(1, 15) = strArray(24).Substring(2, 5).Trim

            Me.vResult(0, 16) = "#MON"
            Me.vResult(1, 16) = strArray(25).Substring(2, 5).Trim

            Me.vResult(0, 17) = "#GRA"
            Me.vResult(1, 17) = strArray(26).Substring(2, 5).Trim

            ' Kiem tra QC
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If Me.spGetDataToView(Me.vResult(1, iCount), 0) = "0" Then
                    iQC = iQC + 1
                End If
            Next

            If iQC >= 10 Then
                ' La QC
                Exit Function
            End If

            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    Me.vResult(2, iCount) = ""
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount) Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(1, iCount) = Me.spGetDataToView(Me.vResult(1, iCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next

                Next

            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

            Dim pTestDetailID As Int64
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            Return True
        Catch ex As Exception
            IO.File.WriteAllText("C:\LOG\Advia60" & "_" & Now.Year.ToString & Now.Month.ToString & Now.Day & "_" & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & Now.Millisecond & "_Error.txt", strInput)
            Return False
        End Try


    End Function

    Public Function spCELTACALPHA(ByVal strInput As String, _
                                  ByVal pTestTypeID As Int16, _
                                  ByVal PortNum As Int16) As Boolean
        Try
            Dim strArray() As String


            Dim f As File
            'strInput = f.ReadAllText("C:\LOG\hungnd\centax.txt")


            strArray = Split(strInput.Substring(1), Chr(13))

            'Dim strResult(1, 16) As String
            Dim iCount, iPara, iQC As Int16
            Dim Test_ID, Patient_ID As Int64
            Dim pTestDate, pTestSeq, pBarcode As String
            Dim cmd As New SqlCommand


            pTestDate = strArray(0).Trim

            ' Xu ly thang nam
            Dim d As Date
            Try
                d = Date.Parse(pTestDate)
            Catch ex As Exception
                f.WriteAllText("C:\LOG\celtax_date.txt", strInput)
                d = Date.Now
            End Try

            pTestDate = Format(d.Day, "0#") & "/" & Format(d.Month, "0#") & "/" & d.Year

            pBarcode = Me.spGetBarcode(strArray(1).Trim)
            pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode


            tblParaName = Me.spGetParaInfor(PortNum)
            ReDim Me.vResult(5, 17)


            Me.vResult(0, 0) = "WBC"
            Me.vResult(1, 0) = strArray(2).Trim

            Me.vResult(0, 1) = "RBC"
            Me.vResult(1, 1) = strArray(9).Trim

            Me.vResult(0, 2) = "HGB"
            Me.vResult(1, 2) = strArray(10).Trim

            Me.vResult(0, 3) = "HCT"
            Me.vResult(1, 3) = strArray(11).Trim

            Me.vResult(0, 4) = "MCV"
            Me.vResult(1, 4) = strArray(12).Trim

            Me.vResult(0, 5) = "MCH"
            Me.vResult(1, 5) = strArray(13).Trim

            Me.vResult(0, 6) = "MCHC"
            Me.vResult(1, 6) = strArray(14).Trim

            Me.vResult(0, 7) = "RDW"
            Me.vResult(1, 7) = strArray(15).Trim

            Me.vResult(0, 8) = "PLT"
            Me.vResult(1, 8) = strArray(16).Trim


            Me.vResult(0, 10) = "PCT"
            Me.vResult(1, 10) = strArray(17).Trim

            Me.vResult(0, 9) = "MPV"
            Me.vResult(1, 9) = strArray(18).Trim

            Me.vResult(0, 11) = "PDW"
            Me.vResult(1, 11) = strArray(19).Trim

            Me.vResult(0, 12) = "%LYM"
            Me.vResult(1, 12) = strArray(3).Trim

            Me.vResult(0, 13) = "%MON"
            Me.vResult(1, 13) = strArray(4).Trim

            Me.vResult(0, 14) = "%GRA"
            Me.vResult(1, 14) = strArray(5).Trim

            Me.vResult(0, 15) = "#LYM"
            Me.vResult(1, 15) = strArray(6).Trim

            Me.vResult(0, 16) = "#MON"
            Me.vResult(1, 16) = strArray(7).Trim

            Me.vResult(0, 17) = "#GRA"
            Me.vResult(1, 17) = strArray(8).Trim


            If Me.tblParaName Is Nothing Then
                For iCount = 0 To Me.vResult.GetLength(1) - 1
                    Me.vResult(2, iCount) = ""
                    Me.vResult(3, iCount) = ""
                    Me.vResult(4, iCount) = ""
                    Me.vResult(5, iCount) = 0
                Next
            Else
                For iCount = 0 To Me.vResult.GetLength(1) - 1

                    If Me.vResult(1, iCount).Substring(Me.vResult(1, iCount).Length - 1).ToUpper = "H" Or Me.vResult(1, iCount).Substring(Me.vResult(1, iCount).Length - 1).ToUpper = "L" Then
                        Me.vResult(1, iCount) = Me.vResult(1, iCount).Substring(0, Me.vResult(1, iCount).Length - 1)
                    End If

                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount) Then
                            Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(1, iCount) = Me.spGetDataToView(Me.vResult(1, iCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                            Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next

                Next

            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            Dim pTestDetailID As Int64
            Dim iParaCount As Int16

            '' Neu chua nhap SID
            'If Test_ID = 0 Then
            '    For iParaCount = 0 To Me.vResult.GetLength(1) - 1
            '        If spCheckBarcodeTemp(pBarcode, Me.vResult(0, iParaCount), pTestDetailID) Then
            '            ' Neu da co du lieu nay
            '            Me.spUpdateResultTemp(pTestDetailID, pBarcode, Me.vResult(1, iParaCount))
            '        Else
            '            Me.spInsertTemp(pBarcode, pTestSeq, pTestTypeID, pTestDate, Me.vResult(5, iParaCount), Me.vResult(0, iParaCount), Me.vResult(1, iParaCount), Me.vResult(2, iParaCount), Me.vResult(3, iParaCount), Me.vResult(4, iParaCount))
            '        End If
            '    Next
            '    Exit Function
            'End If


            For iCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                End If
            Next
            Return True
        Catch ex As Exception
            Return False
        End Try


    End Function

    Public Function SpCelldyn3200(ByVal strInput As String, _
                                      ByVal pTestTypeID As Int16, _
                                      ByVal PortNum As Int16) As Boolean

        Try
            Dim prvResult(5, 0) As String
            Dim strArray() As String
            Dim prvTblParaName As DataTable
            strArray = Split(strInput, ",")
            If strArray(0).Split(Chr(34))(1).Trim = String.Empty Then
                Try
                    If Not Directory.Exists("C:\LOG\Celldyn3200") Then Directory.CreateDirectory("C:\LOG\Celldyn3200")
                    File.WriteAllText("C:\LOG\Celldyn3200\Celldyn3200-" & Now.ToString("dd-MM-yyyy H-mm-s") & ".txt", strInput)
                Catch ex As Exception

                End Try

                Dim iCount, iPara As Int16
                Dim testId, patientId As Int64
                Dim pTestDate, pTestSeq, pBarcode As String
                pTestDate = strArray(13)
                ' Xu ly thang nam
                Dim strDay As String = pTestDate.Substring(4, 2)
                Dim strMonth As String = pTestDate.Substring(1, 2)
                Dim strYear As String = pTestDate.Substring(7, 4)

                pTestDate = strDay & "/" & strMonth & "/" & strYear

                pBarcode = Me.spGetBarcode(strArray(6).Substring(1, 12).Trim)
                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode

                prvTblParaName = Me.spGetParaInfor(PortNum)
                ReDim prvResult(5, 19)

                prvResult(0, 0) = "WBC"
                prvResult(1, 0) = strArray(18).Trim

                prvResult(0, 1) = "NEU"
                prvResult(1, 1) = strArray(19).Trim

                prvResult(0, 2) = "LYM"
                prvResult(1, 2) = strArray(20).Trim

                prvResult(0, 3) = "MONO"
                prvResult(1, 3) = strArray(21).Trim

                prvResult(0, 4) = "EOS"
                prvResult(1, 4) = strArray(22).Trim

                prvResult(0, 5) = "BASO"
                prvResult(1, 5) = strArray(23).Trim

                prvResult(0, 6) = "RBC"
                prvResult(1, 6) = strArray(24).Trim

                prvResult(0, 7) = "HGB"
                Try
                    prvResult(1, 7) = strArray(25).Trim * 10
                Catch ex As Exception
                    prvResult(1, 7) = strArray(25).Trim
                End Try


                prvResult(0, 8) = "HCT"
                Try
                    prvResult(1, 8) = strArray(26).Trim / 100
                Catch ex As Exception
                    prvResult(1, 8) = strArray(26).Trim
                End Try


                prvResult(0, 10) = "MCV"
                prvResult(1, 10) = strArray(27).Trim

                prvResult(0, 9) = "MCH"
                prvResult(1, 9) = strArray(28).Trim

                prvResult(0, 11) = "MCHC"
                Try
                    prvResult(1, 11) = strArray(29).Trim * 10
                Catch ex As Exception
                    prvResult(1, 11) = strArray(29).Trim
                End Try


                prvResult(0, 12) = "RDW"
                prvResult(1, 12) = strArray(30).Trim

                prvResult(0, 13) = "PLT"
                prvResult(1, 13) = strArray(31).Trim

                prvResult(0, 14) = "MPV"
                prvResult(1, 14) = strArray(32).Trim

                prvResult(0, 15) = "%N"
                prvResult(1, 15) = strArray(35).Trim

                prvResult(0, 16) = "%L"
                prvResult(1, 16) = strArray(36).Trim

                prvResult(0, 17) = "%M"
                prvResult(1, 17) = strArray(37).Trim

                prvResult(0, 18) = "%E"
                prvResult(1, 18) = strArray(38).Trim

                prvResult(0, 19) = "%B"
                prvResult(1, 19) = strArray(39).Trim

                If prvTblParaName Is Nothing Then
                    For iCount = 0 To prvResult.GetLength(1) - 1
                        prvResult(2, iCount) = ""
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                    Next
                Else
                    For iCount = 0 To prvResult.GetLength(1) - 1

                        If prvResult(1, iCount).EndsWith("H") Or prvResult(1, iCount).EndsWith("L") Or prvResult(1, iCount).EndsWith("h") Or prvResult(1, iCount).EndsWith("l") Then
                            prvResult(1, iCount) = prvResult(1, iCount).Substring(0, prvResult(1, iCount).Length - 1)
                        End If

                        For iPara = 0 To prvTblParaName.Rows.Count - 1
                            If prvTblParaName.Rows(iPara).Item("Alias_name") = prvResult(0, iCount) Then
                                prvResult(0, iCount) = prvTblParaName.Rows(iPara).Item("Data_name")
                                prvResult(1, iCount) = Me.spGetDataToView(prvResult(1, iCount), prvTblParaName.Rows(iPara).Item("Data_Point"))
                                prvResult(2, iCount) = prvTblParaName.Rows(iPara).Item("Measure_Unit")
                                prvResult(3, iCount) = prvTblParaName.Rows(iPara).Item("Normal_Level")
                                prvResult(4, iCount) = prvTblParaName.Rows(iPara).Item("Normal_LevelW")
                                prvResult(5, iCount) = prvTblParaName.Rows(iPara).Item("Data_Sequence")
                            End If
                        Next
                    Next
                End If

                'Ghi lại log kết quả
                Dim dt As DataTable = lablinkhelper.Utilities.ConvertToDataTable(prvResult)
                If Not Directory.Exists("C:\LOG\Celldyn3200\Result") Then Directory.CreateDirectory("C:\LOG\Celldyn3200\Result")
                lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\Celldyn3200\Result\Celldyn3200-" & Now.ToString("dd-MM-yyyy H-mm-s") & "-" & pBarcode & ".txt")

                Me.spGetID(pBarcode, pTestTypeID, testId, patientId)
                Dim pTestDetailID As Int64

                For iCount = 0 To prvResult.GetLength(1) - 1
                    If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                        Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                    Else
                        Me.SpInsertResult(testId, patientId, pTestTypeID, pTestSeq, pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                    End If
                Next
                Return True
            End If
        Catch ex As Exception
            File.AppendAllText("C:\LOG\Celldyn3200\CellDyn3200_Error.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf)
            Return False
        End Try


    End Function

    Public Function spAdvia1650(ByVal strInput() As String, _
                                      ByVal pTestTypeID As Int16, _
                                      ByVal PortNum As Int16) As Boolean
        'Dim f As File
        Dim cls As New Advia1650
        Dim clsControl As New clsControls
        Dim pTestDate As String
        Dim PatientResult As New Advia1650.Patient
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim iParaCount, iPos As Int16
        ' Duyet tung benh nhan
        For Each s As String In strInput
            If s.Length > 0 Then
                If s(0) = "R" Then
                    cls.ResultDataParse(s, PatientResult)

                    Me.tblParaName = Me.spGetParaInfor(PortNum)

                    pTestDate = PatientResult.DateTestCompleted
                    ' Xu ly khong su dung barcode
                    If PatientResult.SID.Length < 5 Then
                        PatientResult.SID = pTestDate.Substring(6, 2) & pTestDate.Substring(4, 2) & pTestDate.Substring(2, 2) & Me.spGetintOder(pTestTypeID) & spGetBarcode(PatientResult.SID)
                    End If
                    pTestDate = PatientResult.DateTestCompleted.Substring(6, 2) & "/" & PatientResult.DateTestCompleted.Substring(4, 2) & "/" & PatientResult.DateTestCompleted.Substring(0, 4)

                    Me.spGetID(PatientResult.SID, pTestTypeID, Test_ID, Patient_ID)

                    ' Khoi tao gia tri cho mang KQ
                    ReDim vResult(5, PatientResult.ResultArray.GetLength(0) - 1)

                    For iParaCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                        ' Lay thong tin tham so
                        If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                            vResult(0, iParaCount) = PatientResult.ResultArray(iParaCount).TestCode
                            vResult(1, iParaCount) = PatientResult.ResultArray(iParaCount).DataValue
                            vResult(2, iParaCount) = ""
                            vResult(3, iParaCount) = ""
                            vResult(4, iParaCount) = ""
                            vResult(5, iParaCount) = 0
                        Else
                            For iPos = 0 To Me.tblParaName.Rows.Count - 1
                                If PatientResult.ResultArray(iParaCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                                    vResult(0, iParaCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                                    vResult(1, iParaCount) = Me.spGetDataToView(PatientResult.ResultArray(iParaCount).DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                                    vResult(2, iParaCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                                    vResult(3, iParaCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                                    vResult(4, iParaCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                                    vResult(5, iParaCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                                    Exit For
                                End If
                            Next

                            If vResult(0, iParaCount) Is Nothing Then
                                vResult(0, iParaCount) = PatientResult.ResultArray(iParaCount).TestCode
                                vResult(1, iParaCount) = PatientResult.ResultArray(iParaCount).DataValue
                                vResult(2, iParaCount) = ""
                                vResult(3, iParaCount) = ""
                                vResult(4, iParaCount) = ""
                                vResult(5, iParaCount) = 0
                            End If
                        End If

                        If spCheckBarcode(PatientResult.SID, Me.vResult(0, iParaCount), pTestDetailID) Then
                            ' Neu da co du lieu nay
                            Me.SpUpdateTestResult(pTestDetailID, PatientResult.SID, Me.vResult(1, iParaCount))
                        Else
                            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, " ", pTestDate, vResult(5, iParaCount), PatientResult.SID, Me.vResult(0, iParaCount), Me.vResult(1, iParaCount), Me.vResult(2, iParaCount), Me.vResult(3, iParaCount), Me.vResult(4, iParaCount))
                        End If
                        clsControl.SpUpdateRegTestList(PatientResult.SID, Me.vResult(0, iParaCount), Test_ID)
                    Next iParaCount

                End If
            End If
        Next
        Return True
    End Function

    Sub spCentaur(ByVal strInput() As String, _
                   ByVal pTestTypeID As Int16, _
                   ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsCentaur As New Centaur
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New Centaur.Patient()
        Dim iAspect As Byte = 5
        Dim f As File


        'strInput = f.ReadAllLines("C:\LOG\CentaurRecords.log")

        clsCentaur.ResultDataParse(strInput, PatientResult)
        ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
        '0: Name; 
        '1: Gia(tri)
        '2: Don vi; 
        '3: muc thong thuong cho Nam
        '4: Muc thong thuong cho nu
        '5: Data sequence
        '6: Test Date
        'Xet tung Test
        pTestDate = PatientResult.ResultArray(0).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(0).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(0).DateTestCompleted.Substring(0, 4)
        pBarcode = PatientResult.SpecimenID(0)
        'If pBarcode.Length > 0 Then
        '    Dim i As Byte
        '    For i = 0 To pBarcode.Length - 1
        '        If Char.IsDigit(pBarcode(i)) Then
        '            Exit For
        '        End If
        '    Next
        '    If i < pBarcode.Length Then
        '        pBarcode = pBarcode.Substring(i)
        '    End If
        'End If

        'If pBarcode.Length < 5 Then
        '    pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
        'End If
        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
        ' Xet tung ket qua
        For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
            Me.tblParaName = Me.spGetParaInfor(PortNum)
            'pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted
            'pTestDate = pTestDate.Substring(0, 8)
            pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode

                ' Lay du lieu dau tien trong chuoi KQ
                iAspect = clsCentaur.GetDataWithoutDataType(PatientResult.ResultArray(iCount).DataValue)

                vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
                vResult(2, iCount) = ""
                vResult(3, iCount) = ""
                vResult(4, iCount) = ""
                vResult(5, iCount) = 0
                vResult(6, iCount) = pTestDate
            Else
                For iPos = 0 To Me.tblParaName.Rows.Count - 1
                    If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then

                        'Xu ly kieu data_type
                        Try
                            iAspect = Me.tblParaName.Rows(iPos).Item("Data_Type")
                        Catch ex As Exception
                            iAspect = clsCentaur.GetDataWithoutDataType(PatientResult.ResultArray(iCount).DataValue)
                        End Try

                        vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                        vResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue(iAspect), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                        vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                        vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                        vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                        vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                        vResult(6, iCount) = pTestDate
                        Exit For
                    End If
                Next iPos

                If vResult(0, iCount) Is Nothing Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    iAspect = clsCentaur.GetDataWithoutDataType(PatientResult.ResultArray(iCount).DataValue)
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                End If
            End If
            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                ' Neu da co du lieu nay
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
            Else
                ' cap nhat vao CSDL
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
            End If
            'Next iAspect
            clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
        Next iCount
    End Sub

    Sub spELECSYS2010(ByVal strInput() As String, _
                   ByVal pTestTypeID As Int16, _
                   ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsELECSYS2010 As New ELECSYS2010
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New PatientInformation
        Dim dem As Integer
        Dim f As File

        Try
            clsELECSYS2010.ResultDataParse(strInput, PatientResult)
            Dim tmp As New PatientInformation.ResultStruct
            'Lấy ngày trả kết quả là ngày tháng theo máy XN, còn năm thì lấy theo hiện tại
            pTestDate = PatientResult.ResultArray(0).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(0).DateTestCompleted.Substring(4, 2) & "/" & Date.Today.Year.ToString
            pBarcode = PatientResult.SpecimenID
            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua
            For Each tmp In PatientResult.ResultArray
                ReDim vResult(6, PatientResult.ResultArray.Count)
                Me.tblParaName = Me.spGetParaInfor(PortNum)
                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = tmp.TestCode
                    vResult(1, iCount) = tmp.DataValue
                    vResult(2, iCount) = ""
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If tmp.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            'vResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(1, iCount) = Me.spGetDataToView(tmp.DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = tmp.TestCode
                        vResult(1, iCount) = tmp.DataValue
                        vResult(2, iCount) = tmp.Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If

                If pBarcode.Length <= 4 Then
                    pBarcode = spGetBarcode(pBarcode)
                    'pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = Date.Today.Year.ToString & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    'pBarcode = Date.Today.Year.ToString.Substring(2) & Format(Date.Today.Month, "0#") & Format(Date.Today.Day, "0#") & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    pBarcode = pBarcode.Substring(2)
                End If

                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next

            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\Elecsys2010_Input.txt", strInput(dem))
                f.AppendAllText("C:\LOG\Elecsys2010_Input.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\Elecsys2010_Input.txt", Now.ToString & vbCrLf)
            f.AppendAllText("C:\LOG\Elecsys2010_Input.txt", "--------------------------------------------------------------------------------------------------")
            f.AppendAllText("C:\LOG\Elecsys2010_Input.txt", vbCrLf)

        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\Elecsys2010_Exception.txt", strInput(dem))
                f.AppendAllText("C:\LOG\Elecsys2010_Exception.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\Elecsys2010_Exception.txt", vbCrLf & ex.ToString & vbCrLf & Now.ToString & "--------------------------------------------------" & vbCrLf)
        End Try
    End Sub

    Sub spAU400(ByVal strInput As String, _
               ByVal pTestTypeID As Int16, _
               ByVal PortNum As Int16, _
               ByVal Rerun As Boolean)
        If strInput.Trim.EndsWith("E0") Then
            Return
        End If

        Dim clsControl As New clsControls
        Dim clsAU400 As New AU400
        Dim pBarcode, pTestDate As String
        Dim iPos As Int16
        Dim iCount As Integer = 0
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New PatientInformation
        Dim iAccount As Integer = 1
        Dim f As File
        Dim prvResult(6, 0) As String

        Try
            clsAU400.ResultDataParse(strInput, PatientResult, Rerun)
            Console.WriteLine(strInput)
            ReDim prvResult(6, PatientResult.ResultArray.Count)
            Dim tmp As New PatientInformation.ResultStruct
            Dim tmp1 As New PatientInformation.ResultStruct
            'tmp = PatientResult.ResultArray(0)
            pTestDate = strInput.Substring(44, 20).Trim
            'Nếu như không có thông tin ngày tháng năm thì mặc định là ngày tháng năm hiện tại
            If pTestDate = "" Then
                pTestDate = Format(Now, "dd/MM/yyyy")
            End If
            pBarcode = PatientResult.SpecimenID
            If pBarcode.Length <= 4 Then
                pBarcode = Me.spGetBarcode(pBarcode)
                'pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode
                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & pBarcode
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua
            For Each tmp In PatientResult.ResultArray
                ReDim prvResult(6, PatientResult.ResultArray.Count)
                'Xet tung Test
                Me.tblParaName = Me.spGetParaInfor(PortNum)

                'Xử lý LDL trực tiếp, chuyển test code về thành LDL gián tiếp
                iAccount = Get_Account_AU400()
                If iAccount = 1 Then
                    If tmp.TestCode = "84" Then
                        tmp.TestCode = "71"
                    End If
                End If

                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    prvResult(0, iCount) = tmp.TestCode
                    ' Lay du lieu dau tien trong chuoi KQ
                    prvResult(1, iCount) = tmp.DataValue
                    prvResult(2, iCount) = ""
                    prvResult(3, iCount) = ""
                    prvResult(4, iCount) = ""
                    prvResult(5, iCount) = 0
                    prvResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If tmp.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            prvResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            'prvResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(1, iCount) = Me.spGetDataToView(tmp.DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            prvResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            prvResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            prvResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            prvResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If prvResult(0, iCount) Is Nothing Then
                        prvResult(0, iCount) = tmp.TestCode
                        prvResult(1, iCount) = tmp.DataValue
                        prvResult(2, iCount) = tmp.Unit
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                        prvResult(6, iCount) = pTestDate
                    End If
                End If
                Me.spInsertUpdateResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount), PortNum)
                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
                clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), Test_ID)
            Next
            f.AppendAllText("C:\AU400Input.txt", strInput & "---" & Now.ToString & vbCrLf)
        Catch ex As Exception
            f.AppendAllText("C:\AU400Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try

    End Sub

    Sub spAU640(ByVal strInput As String, _
               ByVal pTestTypeID As Int16, _
               ByVal PortNum As Int16, _
               ByVal Rerun As Boolean)
        If strInput.Trim.EndsWith("E0") Then
            Return
        End If

        Dim clsControl As New clsControls
        Dim clsAU400 As New AU400
        Dim pBarcode, pTestDate As String
        Dim iPos As Int16
        Dim iCount As Integer = 0
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New PatientInformation
        Dim iAccount As Integer = 1
        Dim f As File
        Dim prvResult(6, 0) As String

        Try
            clsAU400.ResultDataParse(strInput, PatientResult, Rerun)
            Console.WriteLine(strInput)
            ReDim prvResult(6, PatientResult.ResultArray.Count)
            Dim tmp As New PatientInformation.ResultStruct
            Dim tmp1 As New PatientInformation.ResultStruct
            'tmp = PatientResult.ResultArray(0)
            pTestDate = strInput.Substring(44, 20).Trim
            'Nếu như không có thông tin ngày tháng năm thì mặc định là ngày tháng năm hiện tại
            If pTestDate = "" Then
                pTestDate = Format(Now, "dd/MM/yyyy")
            End If
            pBarcode = PatientResult.SpecimenID
            If pBarcode.Length <= 4 Then
                pBarcode = Me.spGetBarcode(pBarcode)
                'pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode
                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & pBarcode
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua
            For Each tmp In PatientResult.ResultArray
                ReDim prvResult(6, PatientResult.ResultArray.Count)
                'Xet tung Test
                Me.tblParaName = Me.spGetParaInfor(PortNum)

                'Xử lý LDL trực tiếp, chuyển test code về thành LDL gián tiếp
                iAccount = Get_Account_AU400()
                If iAccount = 1 Then
                    If tmp.TestCode = "84" Then
                        tmp.TestCode = "71"
                    End If
                End If

                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    prvResult(0, iCount) = tmp.TestCode
                    ' Lay du lieu dau tien trong chuoi KQ
                    prvResult(1, iCount) = tmp.DataValue
                    prvResult(2, iCount) = ""
                    prvResult(3, iCount) = ""
                    prvResult(4, iCount) = ""
                    prvResult(5, iCount) = 0
                    prvResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If tmp.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            prvResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            'prvResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(1, iCount) = Me.spGetDataToView(tmp.DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            prvResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            prvResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            prvResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            prvResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If prvResult(0, iCount) Is Nothing Then
                        prvResult(0, iCount) = tmp.TestCode
                        prvResult(1, iCount) = tmp.DataValue
                        prvResult(2, iCount) = tmp.Unit
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                        prvResult(6, iCount) = pTestDate
                    End If
                End If
                Me.spInsertUpdateResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount), PortNum)
                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
                clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), Test_ID)
            Next
            f.AppendAllText("C:\AU640Input.txt", strInput & "---" & Now.ToString & vbCrLf)
        Catch ex As Exception
            f.AppendAllText("C:\AU640Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try

    End Sub

    Sub spCA550(ByVal strInput As String, _
               ByVal pTestTypeID As Int16, _
               ByVal PortNum As Int16, _
               ByVal Rerun As Boolean)


        Dim clsControl As New clsControls
        Dim clsCA550 As New CA550
        Dim pBarcode, pTestDate As String
        Dim iPos As Int16
        Dim iCount As Integer = 0
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New PatientInformation
        Dim iAccount As Integer = 1
        Dim f As File
        Try
            clsCA550.ResultDataParse(strInput, PatientResult)
            ReDim vResult(6, PatientResult.ResultArray.Count)
            Dim tmp As New PatientInformation.ResultStruct
            Dim tmp1 As New PatientInformation.ResultStruct
            'tmp = PatientResult.ResultArray(0)
            pTestDate = strInput.Substring(9, 6).Trim
            'Nếu như không có thông tin ngày tháng năm thì mặc định là ngày tháng năm hiện tại
            If pTestDate = "" Then
                pTestDate = Format(Now, "dd/MM/yyyy")
            Else
                pTestDate = pTestDate.Substring(4, 2) & "/" & pTestDate.Substring(2, 2) & "/20" & pTestDate.Substring(0, 2)
            End If
            pBarcode = PatientResult.SpecimenID
            If pBarcode.Length <= 4 Then
                pBarcode = Me.spGetBarcode(pBarcode)
                pBarcode = pTestDate.Substring(0, 2) & pTestDate.Substring(2, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua
            For Each tmp In PatientResult.ResultArray
                ReDim vResult(6, PatientResult.ResultArray.Count)
                'Xet tung Test
                Me.tblParaName = Me.spGetParaInfor(PortNum)

                'Xử lý LDL trực tiếp, chuyển test code về thành LDL gián tiếp
                'iAccount = Get_Account_AU400()
                'If iAccount = 1 Then
                '    If tmp.TestCode = "84" Then
                '        tmp.TestCode = "71"
                '    End If
                'End If

                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = tmp.TestCode
                    ' Lay du lieu dau tien trong chuoi KQ
                    vResult(1, iCount) = tmp.DataValue
                    vResult(2, iCount) = ""
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If tmp.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            'vResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(1, iCount) = Me.spGetDataToView(tmp.DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = tmp.TestCode
                        vResult(1, iCount) = tmp.DataValue
                        vResult(2, iCount) = tmp.Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If

                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next
            f.AppendAllText("C:\LOG\CA550Input.txt", strInput & "---" & Now.ToString & vbCrLf)
        Catch ex As Exception
            f.AppendAllText("C:\LOG\CA550Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try

    End Sub

    Sub spAU480(ByVal strInput As String, _
           ByVal pTestTypeID As Int16, _
           ByVal PortNum As Int16, _
           ByVal Rerun As Boolean)
        Dim clsControl As New clsControls
        Dim clsAU480 As New AU480
        Dim pBarcode, pTestDate As String
        Dim iPos As Int16
        Dim iCount As Integer = 0
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New PatientInformation
        Dim iAccount As Integer = 1
        Dim f As File

        'strInput = f.ReadAllText("C:\LOG\AU480.txt")

        Try
            clsAU480.ResultDataParse(strInput, PatientResult, Rerun)
            ReDim vResult(6, PatientResult.ResultArray.Count)
            Dim tmp As New PatientInformation.ResultStruct
            Dim tmp1 As New PatientInformation.ResultStruct
            'tmp = PatientResult.ResultArray(0)
            '  pTestDate = strInput.Substring(44, 20).Trim
            'Nếu như không có thông tin ngày tháng năm thì mặc định là ngày tháng năm hiện tại
            If pTestDate = "" Then
                pTestDate = Format(Now, "dd/MM/yyyy")
            End If
            pBarcode = PatientResult.SpecimenID
            If pBarcode.Length <= 4 Then
                pBarcode = Me.spGetBarcode(pBarcode)
                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & pBarcode
            End If
            spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            Dim deviceid As Int64
            'Me.spGetDeviceID(pBarcode, pTestTypeID, Test_ID, deviceid)
            ' Xet tung ket qua
            For Each tmp In PatientResult.ResultArray
                ReDim vResult(6, PatientResult.ResultArray.Count)
                'Xet tung Test
                Me.tblParaName = Me.spGetParaInfor(PortNum)

                'Xử lý LDL trực tiếp, chuyển test code về thành LDL gián tiếp
                iAccount = Get_Account_AU400()
                If iAccount = 1 Then
                    If tmp.TestCode = "84" Then
                        tmp.TestCode = "71"
                    End If
                End If

                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = tmp.TestCode
                    ' Lay du lieu dau tien trong chuoi KQ
                    vResult(1, iCount) = tmp.DataValue.Replace("r", "").Replace("h", "")
                    vResult(2, iCount) = ""
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If tmp.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            vResult(1, iCount) = Me.spGetDataToView(tmp.DataValue, Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = tmp.TestCode
                        vResult(1, iCount) = tmp.DataValue.Replace("r", "").Replace("h", "")
                        vResult(2, iCount) = tmp.Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If

                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next
            f.AppendAllText("C:\LOG\AU480Input.txt", strInput & "---" & Now.ToString & vbCrLf)
        Catch ex As Exception
            f.AppendAllText("C:\LOG\AU480Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
        End Try

    End Sub

    Sub spAxSYM(ByVal strInput() As String, _
                  ByVal pTestTypeID As Int16, _
                  ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsAXSYM As New AxSYM
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New AxSYM.Patient()
        Const iAspect As Byte = 0
        Dim dem As Integer = 0
        Dim f As File

        For dem = 0 To strInput.GetLength(0) - 1
            f.AppendAllText("C:\LOG\AxsymInput.txt", strInput(dem))
            f.AppendAllText("C:\LOG\AxsymInput.txt", vbCrLf)
        Next dem
        f.AppendAllText("C:\LOG\AxsymInput.txt", Now.ToString & vbCrLf)
        f.AppendAllText("C:\LOG\AxsymInput.txt", "--------------------------------------------------------------------------------------------------")
        f.AppendAllText("C:\LOG\AxsymInput.txt", vbCrLf)

        Try
            'Lấy về DeviceId từ PortId

            clsAXSYM.ResultDataParse(strInput, PatientResult)
            ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
            '0: Name; 
            '1: Gia(tri)
            '2: Don vi; 
            '3: muc thong thuong cho Nam
            '4: Muc thong thuong cho nu
            '5: Data sequence
            '6: Test Date
            'Xet tung Test

            'pTestDate = PatientResult.ResultArray(0).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(0).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(0).DateTestCompleted.Substring(0, 4)
            'pBarcode = PatientResult.SpecimenID(0)
            'If pBarcode.Length < 5 Then
            '    pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            'End If
            'Me.spGetID(pBarcode, Test_ID, Patient_ID)

            ' Xet tung ket qua
            Me.tblParaName = Me.spGetParaInfor(PortNum)
            For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                'pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted
                'pTestDate = pTestDate.Substring(0, 8)

                pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                pBarcode = PatientResult.ResultArray(iCount).BarcodeTest
                If pBarcode.Length < 5 Then
                    pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                End If
                Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
                    vResult(2, iCount) = ""
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
                            vResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue(iAspect), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                        vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
                        vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    ' cap nhat vao CSDL
                    Dim DeviceId As Integer = lablinkhelper.Utilities.GetDeviceIdFromPortId(PortNum)
                    GetInforByTestCode(DeviceId, pBarcode, PatientResult.ResultArray(iCount).TestCode, Patient_ID, Test_ID, pTestTypeID)
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                'Next iAspect
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next iCount
        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\AxsymException.txt", strInput(dem))
                f.AppendAllText("C:\LOG\AxsymException.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\AxsymException.txt", vbCrLf & ex.ToString & vbCrLf & Now.ToString & "--------------------------------------------------" & vbCrLf)
        End Try
    End Sub

    Private Sub GetInforByTestCode(ByVal deviceId As Integer, ByVal pBarcode As String, ByVal testCode As String, ByRef pPatientId As Long, ByRef testID As Long, ByRef pTestTypeID As Short)
        Try
            Dim testAlias As String = GetFieldValue("D_DATA_CONTROL", "TestData_ID", String.Format(" Device_ID = {0} And Alias_Name = '{1}'", deviceId, testCode))
            If Not String.IsNullOrEmpty(testAlias) Then
                testID = GetFieldValue("T_REG_LIST", "Test_ID", String.Format("TestData_ID = '{0}' And Barcode = '{1}'", testAlias, pBarcode))
                pPatientId = GetFieldValue("T_TEST_INFO", "Patient_ID", String.Format("Test_ID = {0}", testID))
                pTestTypeID = GetFieldValue("L_Standard_Test", "TestType_ID", String.Format("TestData_ID = '{0}' ", testAlias))

            End If
        Catch ex As Exception

        End Try


    End Sub


    Sub spCI4100(ByVal strInput() As String, ByVal pTestTypeID As Int16, ByVal PortNum As Int16)

        Dim prvResult(5, 0) As String
        Dim clsControl As New clsControls
        Dim clsCi4100 As New CI4100
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim testID, patientID, pTestDetailID As Int64
        Dim patientResult As New CI4100.Patient()
        Dim prvTblParaName As DataTable
        Const iAspect As Byte = 0
        Dim dem As Integer = 0

        Dim resultArray As New ArrayList()
        Dim sArray As New ArrayList()

        For dem = 0 To strInput.GetLength(0) - 1
            File.AppendAllText("C:\LOG\CI4100Input.txt", strInput(dem))
            File.AppendAllText("C:\LOG\CI4100Input.txt", vbCrLf)
        Next dem
        File.AppendAllText("C:\LOG\CI4100Input.txt", Now.ToString & vbCrLf)
        File.AppendAllText("C:\LOG\CI4100Input.txt", "-----------------------------------------------------------------------------------------------")
        File.AppendAllText("C:\LOG\CI4100Input.txt", vbCrLf)

        Try

            For i As UInt16 = 0 To strInput.Length - 1
                If strInput(i).Trim() = "" Then Continue For
                Dim s() As String = strInput(i).Split(CI4100.FieldDelimiter)
                If s(0) = "H" Then
                    sArray = New ArrayList()
                    sArray.Clear()
                End If
                sArray.Add(strInput(i))
                If s(0) = "L" Then
                    resultArray.Add(sArray)
                End If
            Next

            For Each sArray In resultArray
                clsCi4100.ResultDataParse(sArray, patientResult)
                ReDim prvResult(6, patientResult.ResultArray.GetLength(0))

                ' Xet tung ket qua
                For iCount = 0 To patientResult.ResultArray.GetLength(0) - 1
                    pTestDate = patientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & patientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & patientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                    'pTestDate = Format(Date.Today.Day, "0#") & "/" & Format(Date.Today.Month, "0#") & "/" & Date.Today.Year.ToString
                    pBarcode = patientResult.ResultArray(iCount).BarcodeTest
                    pBarcode = pBarcode.Replace(".", "").Replace(",", "").Replace("<", "").Replace(">", "")
                    If pBarcode.Length < 5 Then
                        pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
                    End If

                    'Fix lỗi lấy nhầm TestId
                    Me.spGetID(pBarcode, pTestTypeID, testID, patientID) 'pTestTypeID

                    Dim moduleName As String = patientResult.ResultArray(iCount).WhichModule.Trim.ToUpper

                    Dim machinesName As String
                    Try
                        machinesName = File.ReadAllText("CI4100_Machines.txt")
                    Catch ex As Exception
                        machinesName = ""
                        File.WriteAllText("CI4100_Machines.txt", "")
                    End Try

                    'If (ModuleName = "C400098") Or (ModuleName = "I1SR01992") Then
                    If machinesName.IndexOf(moduleName) >= 0 Then
                        Dim strPortNum, strTestTypeID
                        strPortNum = GetFieldValue("D_DEVICE_LIST", "Port_ID", "Device_Name = '" & moduleName & "'")
                        strTestTypeID = GetFieldValue("D_DEVICE_LIST", "TestType_ID", "Device_Name = '" & moduleName & "'")
                        Try
                            PortNum = CInt(strPortNum)
                            pTestTypeID = CInt(strTestTypeID)
                        Catch ex As Exception
                        End Try
                    End If

                    prvTblParaName = Me.spGetParaInfor(PortNum)

                    If prvTblParaName Is Nothing Or prvTblParaName.Rows.Count = 0 Then
                        prvResult(0, iCount) = patientResult.ResultArray(iCount).TestCode
                        prvResult(1, iCount) = patientResult.ResultArray(iCount).DataValue(iAspect)
                        prvResult(2, iCount) = ""
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                        prvResult(6, iCount) = pTestDate
                    Else
                        For iPos = 0 To prvTblParaName.Rows.Count - 1
                            If patientResult.ResultArray(iCount).TestCode = prvTblParaName.Rows(iPos).Item("Alias_name") Then
                                prvResult(0, iCount) = prvTblParaName.Rows(iPos).Item("Data_name")
                                prvResult(1, iCount) = Me.spGetDataToView(patientResult.ResultArray(iCount).DataValue(iAspect), prvTblParaName.Rows(iPos).Item("Data_Point"))
                                prvResult(2, iCount) = prvTblParaName.Rows(iPos).Item("Measure_Unit")
                                prvResult(3, iCount) = prvTblParaName.Rows(iPos).Item("Normal_Level")
                                prvResult(4, iCount) = prvTblParaName.Rows(iPos).Item("Normal_LevelW")
                                prvResult(5, iCount) = prvTblParaName.Rows(iPos).Item("Data_Sequence")
                                prvResult(6, iCount) = pTestDate
                                Exit For
                            End If
                        Next iPos
                        If prvResult(0, iCount) Is Nothing Then
                            prvResult(0, iCount) = patientResult.ResultArray(iCount).TestCode
                            prvResult(1, iCount) = patientResult.ResultArray(iCount).DataValue(iAspect)
                            prvResult(2, iCount) = patientResult.ResultArray(iCount).Unit
                            prvResult(3, iCount) = ""
                            prvResult(4, iCount) = ""
                            prvResult(5, iCount) = 0
                            prvResult(6, iCount) = pTestDate
                        End If
                    End If
                    If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                        ' Neu da co du lieu nay
                        Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                    Else
                        ' cap nhat vao CSDL
                        Me.SpInsertResult(testID, patientID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                    End If
                    'Next iAspect
                    clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), testID)
                Next iCount
            Next
        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                File.AppendAllText("C:\LOG\CI4100Exception.txt", strInput(dem))
                File.AppendAllText("C:\LOG\CI4100Exception.txt", vbCrLf)
            Next dem
            File.AppendAllText("C:\LOG\CI4100Exception.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & "-----------------------------------------------" & vbCrLf)
        End Try
    End Sub

    ''''Sub spGA400(ByVal strInput() As String, _
    ''''              ByVal pTestTypeID As Int16, _
    ''''              ByVal PortNum As Int16)
    ''''    Dim clsControl As New clsControls
    ''''    Dim clsGA400 As New GA400
    ''''    Dim pBarcode, pTestDate As String
    ''''    Dim iCount, iPos As Int16
    ''''    Dim Test_ID, Patient_ID, pTestDetailID As Int64
    ''''    Dim PatientResult As New GA400.Patient()
    ''''    Const iAspect As Byte = 0
    ''''    Dim dem As Integer = 0
    ''''    Dim f As File

    ''''    For dem = 0 To strInput.GetLength(0) - 1
    ''''        f.AppendAllText("C:\LOG\GA400Input.txt", strInput(dem))
    ''''        f.AppendAllText("C:\LOG\GA400Input.txt", vbCrLf)
    ''''    Next dem
    ''''    f.AppendAllText("C:\LOG\GA400Input.txt", Now.ToString & vbCrLf)
    ''''    f.AppendAllText("C:\LOG\GA400Input.txt", "-----------------------------------------------------------------------------------------------")
    ''''    f.AppendAllText("C:\LOG\GA400Input.txt", vbCrLf)

    ''''    Try
    ''''        clsGA400.ResultDataParse(strInput, PatientResult)
    ''''        ReDim vResult(6, PatientResult.ResultArray.GetLength(0))

    ''''        ' Xet tung ket qua
    ''''        For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1

    ''''            Dim pDay As String = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2)
    ''''            Dim pMonth As String = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2)
    ''''            Dim pYear As String = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)

    ''''            Try
    ''''                Dim intday As Integer = CInt(pDay)
    ''''                Dim intmonth As Integer = CInt(pMonth)
    ''''                Dim intyear As Integer = CInt(pYear)
    ''''                Dim currDate As DateTime = New DateTime(intyear, intmonth, intday)
    ''''                If currDate > New DateTime(2012, 5, 30) Then Return

    ''''            Catch ex As Exception

    ''''            End Try

    ''''            pTestDate = pDay & "/" & pMonth & "/" & pYear
    ''''            pBarcode = PatientResult.ResultArray(iCount).BarcodeTest
    ''''            If pBarcode.Length < 5 Then
    ''''                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
    ''''            End If
    ''''            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

    ''''            Me.tblParaName = Me.spGetParaInfor(PortNum)

    ''''            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
    ''''                vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
    ''''                vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
    ''''                vResult(2, iCount) = ""
    ''''                vResult(3, iCount) = ""
    ''''                vResult(4, iCount) = ""
    ''''                vResult(5, iCount) = 0
    ''''                vResult(6, iCount) = pTestDate
    ''''            Else
    ''''                For iPos = 0 To Me.tblParaName.Rows.Count - 1
    ''''                    If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
    ''''                        vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")
    ''''                        vResult(1, iCount) = Me.spGetDataToView(PatientResult.ResultArray(iCount).DataValue(iAspect), Me.tblParaName.Rows(iPos).Item("Data_Point"))
    ''''                        vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
    ''''                        vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
    ''''                        vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
    ''''                        vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
    ''''                        vResult(6, iCount) = pTestDate
    ''''                        Exit For
    ''''                    End If
    ''''                Next iPos
    ''''                If vResult(0, iCount) Is Nothing Then
    ''''                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
    ''''                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue(iAspect)
    ''''                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
    ''''                    vResult(3, iCount) = ""
    ''''                    vResult(4, iCount) = ""
    ''''                    vResult(5, iCount) = 0
    ''''                    vResult(6, iCount) = pTestDate
    ''''                End If
    ''''            End If
    ''''            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
    ''''                Me.spUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
    ''''            Else
    ''''                Me.spInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
    ''''            End If
    ''''            clsControl.spUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
    ''''        Next iCount
    ''''    Catch ex As Exception
    ''''        For dem = 0 To strInput.GetLength(0) - 1
    ''''            f.AppendAllText("C:\LOG\GA400Exception.txt", strInput(dem))
    ''''            f.AppendAllText("C:\LOG\GA400Exception.txt", vbCrLf)
    ''''        Next dem
    ''''        f.AppendAllText("C:\LOG\GA400Exception.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & "-----------------------------------------------" & vbCrLf)
    ''''    End Try
    ''''End Sub

    Sub spGA400(ByVal strInput() As String, ByVal pTestTypeID As Int16, ByVal PortNum As Int16)
        'strInput = File.ReadAllLines("E:\GA-400 Direct Output.txt")
        Dim pBarcode, pTestDate, pSequence As String
        Dim sDataSegment() As String
        Dim strResult() As String
        Dim iPos, iParaCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim dem As Integer = 0
        For dem = 0 To strInput.GetLength(0) - 1
            File.AppendAllText("C:\LOG\GA400Input.txt", strInput(dem))
            File.AppendAllText("C:\LOG\GA400Input.txt", vbCrLf)
        Next dem
        File.AppendAllText("C:\LOG\GA400Input.txt", Now.ToString & vbCrLf)
        File.AppendAllText("C:\LOG\GA400Input.txt", "-----------------------------------------------------------------------------------------------")
        File.AppendAllText("C:\LOG\GA400Input.txt", vbCrLf)
        'Khai báo các giá trị thông số tính toán
        Dim idCHOL, idTG, idHDLC As Integer
        Dim idTP, idALB As Integer
        Dim idTBIL, idDBIL As Integer
        Dim idCa As Integer
        idCHOL = -1
        idTG = -1
        idHDLC = -1
        idTP = -1
        idALB = -1
        idTBIL = -1
        idDBIL = -1
        idCa = -1
        Dim CHOL, TG, HDLC, TP, ALB, TBIL, DBIL, Ca As Double
        Try
            sDataSegment = strInput
            iPos = -1
            pBarcode = "0000"
            For iParaCount = 0 To sDataSegment.GetLength(0) - 1
                If sDataSegment(iParaCount) <> String.Empty Then
                    Select Case sDataSegment(iParaCount).Substring(0, 1).ToUpper
                        Case "O"
                            Dim ptempBarcode As String() = sDataSegment(iParaCount).Split("|")(2).Split("^")
                            pBarcode = ptempBarcode(0).Trim
                            If pBarcode = "" Then
                                pBarcode = ptempBarcode(0)
                            End If
                            pTestDate = sDataSegment(iParaCount).Split("|")(6)
                        Case "R"
                            iPos = iPos + 1
                            strResult = sDataSegment(iParaCount).Split("|")
                            ReDim Preserve vResult(5, iPos)
                            vResult(0, iPos) = strResult(2).Replace("^", "")
                            vResult(1, iPos) = strResult(3)
                            Dim dname As String = vResult(0, iPos).Trim
                            If (dname = "CHOL") And (idCHOL = -1) Then
                                idCHOL = iPos
                                CHOL = vResult(1, iPos)
                            End If
                            If (dname = "TG") And (idTG = -1) Then
                                idTG = iPos
                                TG = vResult(1, iPos)
                            End If
                            If (dname = "HDL-C") And (idHDLC = -1) Then
                                idHDLC = iPos
                                HDLC = vResult(1, iPos)
                            End If
                            If (dname = "TP") And (idTP = -1) Then
                                idTP = iPos
                                TP = vResult(1, iPos)
                            End If
                            If (dname = "ALB") And (idALB = -1) Then
                                idALB = iPos
                                ALB = vResult(1, iPos)
                            End If
                            If (dname = "TBIL") And (idTBIL = -1) Then
                                idTBIL = iPos
                                TBIL = vResult(1, iPos)
                            End If
                            If (dname = "DBIL") And (idDBIL = -1) Then
                                idDBIL = iPos
                                DBIL = vResult(1, iPos)
                            End If
                            If (dname = "Ca") And (idCa = -1) Then
                                idCa = iPos
                                Ca = vResult(1, iPos)
                            End If
                            vResult(2, iPos) = strResult(4)
                            vResult(3, iPos) = strResult(5).Replace("\^", "").Replace("^", "-")
                            vResult(4, iPos) = strResult(5).Replace("\^", "").Replace("^", "-")
                    End Select
                End If
            Next
            'Xử lý các thông số tính toán nếu có:
            'LDL-C
            Dim idLDLC As Integer = FindInResult("LDL-C")
            Dim sKhongtinhduoc As String = "Không tính được"
            Try
                If (idLDLC = -1) Then
                    'Nếu chưa có thông số thì thực hiện tính toán
                    'Có rồi thì thôi :D
                    'Kiểm tra các thông số liên quan nếu chưa đủ thì out
                    If (idCHOL >= 0) And (idTG >= 0) And (idHDLC >= 0) Then
                        Dim lengresult As Integer = Me.vResult.GetLength(1)
                        ReDim Preserve vResult(5, lengresult)
                        vResult(0, lengresult) = "LDL-C"
                        If TG > 4.5 Then
                            vResult(1, lengresult) = sKhongtinhduoc
                        Else
                            vResult(1, lengresult) = Math.Round(CHOL - ((TG / 2.2) + HDLC), 2).ToString
                        End If
                        vResult(2, lengresult) = "mmol/l"
                        vResult(3, lengresult) = "0.0 - 3.4"
                        vResult(4, lengresult) = vResult(3, lengresult)
                    End If
                Else
                    If TG > 4.5 Then
                        vResult(1, idLDLC) = sKhongtinhduoc
                    End If
                End If
                'GLOB
                If (FindInResult("GLOB") = -1) Then
                    'Nếu chưa có thông số thì thực hiện tính toán
                    'Có rồi thì thôi :D
                    'Kiểm tra các thông số liên quan nếu chưa đủ thì out
                    If (idTP >= 0) And (idALB >= 0) Then
                        Dim lengresult As Integer = Me.vResult.GetLength(1)
                        ReDim Preserve vResult(5, lengresult)
                        vResult(0, lengresult) = "GLOB"
                        vResult(1, lengresult) = Math.Round(TP - ALB, 0).ToString
                        vResult(2, lengresult) = "g/l"
                        vResult(3, lengresult) = "24 - 38"
                        vResult(4, lengresult) = vResult(3, lengresult)
                    End If
                End If
                'A/G
                If (FindInResult("A/G") = -1) Then
                    'Nếu chưa có thông số thì thực hiện tính toán
                    'Có rồi thì thôi :D
                    'Kiểm tra các thông số liên quan nếu chưa đủ thì out
                    If (idTP >= 0) And (idALB >= 0) Then
                        Dim lengresult As Integer = Me.vResult.GetLength(1)
                        ReDim Preserve vResult(5, lengresult)
                        vResult(0, lengresult) = "A/G"
                        vResult(1, lengresult) = Math.Round(ALB / (TP - ALB), 1).ToString
                        vResult(2, lengresult) = ""
                        vResult(3, lengresult) = "1.3 - 1.8"
                        vResult(4, lengresult) = vResult(3, lengresult)
                    End If
                End If
                'BIL-GT
                If (FindInResult("BIL-GT") = -1) Then
                    'Nếu chưa có thông số thì thực hiện tính toán
                    'Có rồi thì thôi :D
                    'Kiểm tra các thông số liên quan nếu chưa đủ thì out
                    If (idTBIL >= 0) And (idDBIL >= 0) Then
                        Dim lengresult As Integer = Me.vResult.GetLength(1)
                        ReDim Preserve vResult(5, lengresult)
                        vResult(0, lengresult) = "BIL-GT"
                        vResult(1, lengresult) = Math.Round(TBIL - DBIL, 1).ToString
                        vResult(2, lengresult) = "umol/l"
                        vResult(3, lengresult) = "0.0 - 12.7"
                        vResult(4, lengresult) = vResult(3, lengresult)
                    End If
                End If
                'Ca-ion
                If (FindInResult("Ca-ion") = -1) Then
                    'Nếu chưa có thông số thì thực hiện tính toán
                    'Có rồi thì thôi :D
                    'Kiểm tra các thông số liên quan nếu chưa đủ thì out
                    If (idCa >= 0) And (idTP >= 0) Then
                        Dim lengresult As Integer = Me.vResult.GetLength(1)
                        ReDim Preserve vResult(5, lengresult)
                        vResult(0, lengresult) = "Ca-ion"
                        vResult(1, lengresult) = Math.Round(((65 * Ca + 11.4) / (TP + 66) - 0.16), 2).ToString
                        vResult(2, lengresult) = "mmol/l"
                        vResult(3, lengresult) = "1.17 - 1.29"
                        vResult(4, lengresult) = vResult(3, lengresult)
                    End If
                End If
            Catch ex As Exception
                File.AppendAllText("C:\LOG\GA400-ErrorTT.txt", vbCrLf & vbCrLf & ex.ToString)
            End Try
            'Xử lý ngày
            pTestDate = pTestDate.Substring(0, 8)
            'Barcode
            pBarcode = pTestDate.Substring(2) & Me.spGetintOder(pTestTypeID) & spGetBarcode(pBarcode)

            Dim pday As String = pTestDate.Substring(6, 2)
            Dim pmonth As String = pTestDate.Substring(4, 2)
            Dim pyear As String = pTestDate.Substring(0, 4)
            pTestDate = pday & "/" & pmonth & "/" & pyear
            Try
                Dim intDay As Integer = CInt(pday)
                Dim intMonth As Integer = CInt(pmonth)
                Dim intYear As Integer = CInt(pyear)
                Dim dDate As DateTime = New DateTime(intYear, intMonth, intDay)
                If dDate > New DateTime(2012, 5, 31) Then Return
            Catch ex As Exception

            End Try


            Me.tblParaName = Me.spGetParaInfor(PortNum)
            Me.spGetID(pBarcode, Test_ID, Patient_ID)
            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                For iParaCount = 0 To vResult.GetLength(1) - 1
                    vResult(5, iParaCount) = 0
                Next
            Else
                For iParaCount = 0 To Me.vResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iParaCount).ToUpper Then
                            Dim tempStr As String = String.Empty
                            Me.vResult(0, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            Me.vResult(1, iParaCount) = Me.spGetDataToView(Me.vResult(1, iParaCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                            tempStr = Me.tblParaName.Rows(iPara).Item("Measure_Unit").ToString.Trim
                            If tempStr <> "" Then
                                Me.vResult(2, iParaCount) = tempStr
                            End If
                            tempStr = Me.tblParaName.Rows(iPara).Item("Normal_Level").ToString.Trim
                            If tempStr <> "" Then
                                Me.vResult(3, iParaCount) = tempStr
                            End If
                            tempStr = Me.tblParaName.Rows(iPara).Item("Normal_LevelW").ToString.Trim
                            If tempStr <> "" Then
                                Me.vResult(4, iParaCount) = tempStr
                            End If
                            Me.vResult(5, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next
                Next
            End If
            For iParaCount = 0 To Me.vResult.GetLength(1) - 1
                If spCheckBarcode(pBarcode, Me.vResult(0, iParaCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iParaCount))
                    'MessageBox.Show("Kết quả đã có trong hệ thống, không cập nhật lại!", "Cập nhật xét nghiệm huyết học")
                Else
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pSequence, pTestDate, Me.vResult(5, iParaCount), pBarcode, Me.vResult(0, iParaCount), Me.vResult(1, iParaCount), Me.vResult(2, iParaCount), Me.vResult(3, iParaCount), Me.vResult(4, iParaCount))
                End If
            Next
        Catch ex As Exception
            Dim errorFileName As String = "C:\LOG\GA-400-Error.txt"
            File.WriteAllLines(errorFileName, strInput)
            File.AppendAllText(errorFileName, vbCrLf & vbCrLf & ex.ToString)
        End Try
    End Sub

    Sub spADVIA2120(ByVal strInput As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)
        Dim prvResult(5, 0) As String
        Dim f As File
        Dim pBarcode, pTestDate, sTemp, pSequence, s As String
        Dim cls As New Advia2120
        Dim sDataSegment() As String
        Dim strResult() As String
        Dim iPos, iParaCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64

        Try
            Try
                If Not Directory.Exists("C:\LOG\Advia2120") Then Directory.CreateDirectory("C:\LOG\Advia2120")
                File.WriteAllText("C:\LOG\Advia2120\Advia2120-" & Now.ToString("dd-MM-yyyy H-mm") & ".txt", strInput)
            Catch ex As Exception

            End Try

            Me.tblParaName = Me.spGetParaInfor(PortNum)

            'sDataSegment = strInput.Substring(3).Split(Chr(2))

            sTemp = strInput.Substring(1)
            pBarcode = sTemp.Substring(0, 14)

            sTemp = sTemp.Substring(15)
            pSequence = sTemp.Substring(0, 6)
            sTemp = sTemp.Substring(6)
            sTemp = sTemp.Substring(11)
            pTestDate = sTemp.Substring(0, 8)
            pTestDate = pTestDate.Substring(0, 6) & "20" & pTestDate.Substring(6, 2)

            Dim tempDateTime() As String = pTestDate.Split("/")
            Try
                Dim tp As DateTime
                tp = New DateTime(Convert.ToInt32(tempDateTime(2)), Convert.ToInt32(tempDateTime(1)), Convert.ToInt32(tempDateTime(0)))
            Catch ex As Exception
                pTestDate = DateTime.Now.ToString("dd/MM/yyyy")
            End Try

            ' Xu ly ky tu 0
            Do While pBarcode.Substring(0, 1) = "0"
                pBarcode = pBarcode.Substring(1)
                If pBarcode.Length = 1 Then
                    Exit Do
                End If
            Loop

            'If pBarcode.Length = 7 Then
            '    pBarcode = "0" & pBarcode
            'End If

            If pBarcode.Substring(0, 1) = "0" And pBarcode.Length = 1 Then
                Exit Sub
            End If

            ' Xu ly mot chieu
            If pBarcode.Length <= 4 Then
                'pBarcode = pBarcode.Substring(6, 4)
                pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(pBarcode)
            End If

            sTemp = sTemp.Substring(8)
            sTemp = sTemp.Substring(9) ' hh:mm:ss
            sTemp = sTemp.Substring(3)
            sTemp = sTemp.Substring(2) ' line feed
            sTemp = sTemp.Substring(0, sTemp.IndexOf(vbCrLf))

            iPos = 0
            Do
                s = sTemp.Substring(0, 9)
                ReDim Preserve prvResult(5, iPos)
                prvResult(0, iPos) = s.Substring(0, 3).Trim
                prvResult(1, iPos) = s.Substring(3, 5).Trim
                iPos += 1
                sTemp = sTemp.Substring(9)
            Loop While (sTemp.Length >= 9)
            iPos = 0




            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                For iParaCount = 0 To prvResult.GetLength(1) - 1
                    prvResult(2, iParaCount) = ""
                    prvResult(3, iParaCount) = ""
                    prvResult(4, iParaCount) = ""
                    prvResult(5, iParaCount) = 0
                Next
            Else
                For iParaCount = 0 To prvResult.GetLength(1) - 1
                    For iPara = 0 To Me.tblParaName.Rows.Count - 1
                        If Me.tblParaName.Rows(iPara).Item("Alias_name") = prvResult(0, iParaCount) Then
                            prvResult(0, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                            'If Me.tblParaName.Rows(iPara).Item("Data_Type") = "1" Then
                            '    Me.prvResult(1, iParaCount) = (Val(Me.prvResult(1, iParaCount)) / 100).ToString
                            'End If

                            prvResult(1, iParaCount) = Me.spGetDataToView(prvResult(1, iParaCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                            prvResult(2, iParaCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                            prvResult(3, iParaCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                            prvResult(4, iParaCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                            prvResult(5, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                        End If
                    Next iPara
                Next iParaCount
            End If

            'Ghi lại log kết quả
            Dim dt As DataTable = lablinkhelper.Utilities.ConvertToDataTable(prvResult)
            If Not Directory.Exists("C:\LOG\Advia2120\Result") Then Directory.CreateDirectory("C:\LOG\Advia2120\Result")
            lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\Advia2120\Result\Advia2120-" & Now.ToString("dd-MM-yyyy H-mm-s") & "-" & pBarcode & ".txt")

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

            For iParaCount = 0 To prvResult.GetLength(1) - 1

                'If spCheckBarcode(pBarcode, prvResult(0, iParaCount), pTestDetailID) Then
                '    ' Neu da co du lieu nay
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iParaCount))
                'Else
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pSequence, pTestDate, prvResult(5, iParaCount), pBarcode, prvResult(0, iParaCount), prvResult(1, iParaCount), prvResult(2, iParaCount), prvResult(3, iParaCount), prvResult(4, iParaCount))
                'End If
                Me.spInsertUpdateResult(Test_ID, Patient_ID, pTestTypeID, pSequence, pTestDate, prvResult(5, iParaCount), pBarcode, prvResult(0, iParaCount), prvResult(1, iParaCount), prvResult(2, iParaCount), prvResult(3, iParaCount), prvResult(4, iParaCount), PortNum)
            Next
            File.AppendAllText("C:\LOG\Advia2120_StrInput_Success.txt", Now.ToString & vbCrLf & strInput & vbCrLf & vbCrLf)
        Catch ex As Exception
            File.AppendAllText("C:\LOG\Advia2120_Exception.txt", Now.ToString & vbCrLf & strInput & vbCrLf & vbCrLf)
        End Try
    End Sub

    Sub spCoulterBeckman(ByVal strInput() As String, _
                         ByVal pTestTypeID As Int16, _
                         ByVal PortNum As Int16)
        Dim f As File
        Dim pBarcode, pTestDate, sTemp, pSequence As String

        Dim cls As New Advia2120
        Dim sDataSegment() As String
        Dim strResult() As String
        Dim iPos, iParaCount, iPara As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        'strInput = f.ReadAllText("C:\LOG\XT1800i_Records.log")


        'sDataSegment = Split(strInput, vbCrLf)

        sDataSegment = strInput
        iPos = 0
        For iParaCount = 0 To sDataSegment.GetLength(0) - 1
            If sDataSegment(iParaCount) <> String.Empty Then

                Select Case sDataSegment(iParaCount).Substring(0, 1).ToUpper
                    Case "O"
                        pBarcode = sDataSegment(iParaCount).Substring(4, 9)
                    Case "R"

                        strResult = sDataSegment(iParaCount).Split("|")

                        If InStr(strResult(2).ToUpper, "HISTO") <= 0 Then
                            ReDim Preserve vResult(5, iPos)
                            vResult(0, iPos) = strResult(2).Replace("!", "")
                            vResult(1, iPos) = strResult(3)
                            If InStr(strResult(3), "!") > 0 Then
                                vResult(1, iPos) = strResult(3).Substring(0, InStr(strResult(3), "!") - 1)
                            End If

                            vResult(2, iPos) = strResult(4)
                            vResult(3, iPos) = strResult(5)
                            vResult(4, iPos) = strResult(5)

                            pTestDate = sDataSegment(iParaCount).Substring(sDataSegment(iParaCount).Length - 14, 14)
                            pTestDate = pTestDate.Substring(0, 8)
                            iPos = iPos + 1
                        End If
                End Select

            End If
        Next

        ' Xu ly mot chieu
        If pBarcode.Substring(0, 5) = "00000" Then
            pBarcode = pBarcode.Substring(5, 4)
            pBarcode = pTestDate.Substring(2) & Me.spGetintOder(pTestTypeID) & pBarcode
        End If

        pTestDate = pTestDate.Substring(6, 2) & "/" & pTestDate.Substring(4, 2) & "/" & pTestDate.Substring(0, 4)


        Me.tblParaName = Me.spGetParaInfor(PortNum)
        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

        If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
            For iParaCount = 0 To vResult.GetLength(0) - 1
                vResult(2, iParaCount) = ""
                vResult(3, iParaCount) = ""
                vResult(4, iParaCount) = ""
                vResult(5, iParaCount) = 0
            Next
        Else
            For iParaCount = 0 To Me.vResult.GetLength(1) - 1
                For iPara = 0 To Me.tblParaName.Rows.Count - 1
                    If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iParaCount).ToUpper Then
                        Me.vResult(0, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                        Me.vResult(1, iParaCount) = Me.spGetDataToView(Me.vResult(1, iParaCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                        Me.vResult(2, iParaCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                        Me.vResult(3, iParaCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                        Me.vResult(4, iParaCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                        Me.vResult(5, iParaCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                    End If
                Next

            Next

        End If
        For iParaCount = 0 To Me.vResult.GetLength(1) - 1
            If spCheckBarcode(pBarcode, Me.vResult(0, iParaCount), pTestDetailID) Then
                ' Neu da co du lieu nay
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iParaCount))
            Else
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pSequence, pTestDate, Me.vResult(5, iParaCount), pBarcode, Me.vResult(0, iParaCount), Me.vResult(1, iParaCount), Me.vResult(2, iParaCount), Me.vResult(3, iParaCount), Me.vResult(4, iParaCount))
            End If
        Next



    End Sub

    Sub spXT1800(ByVal strInput() As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16, _
       ByVal strDeviceName As String)
        Dim clsControl As New clsControls
        Dim clsXT1800 As New XT1800
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New XT1800.Patient()
        Dim dem As Integer
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT.log")
        Try
            If strDeviceName = "CA500" Then
                For dem = 0 To strInput.GetLength(0) - 1
                    f.AppendAllText("C:\LOG\CA500Input.txt", strInput(dem))
                    f.AppendAllText("C:\LOG\CA500Input.txt", vbCrLf)
                Next dem
                f.AppendAllText("C:\LOG\CA500Input.txt", Now.ToString & vbCrLf & vbCrLf)
                clsXT1800.ResultDataParse_CA(strInput, PatientResult)
            Else
                clsXT1800.ResultDataParse(strInput, PatientResult)
                For dem = 0 To strInput.GetLength(0) - 1
                    f.AppendAllText("C:\LOG\XTInput.txt", strInput(dem))
                    f.AppendAllText("C:\LOG\XTInput.txt", vbCrLf)
                Next dem
                f.AppendAllText("C:\LOG\XTInput.txt", Now.ToString & vbCrLf & vbCrLf)
            End If

            ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
            '0: Name; 
            '1: Gia(tri)
            '2: Don vi; 
            '3: muc thong thuong cho Nam
            '4: Muc thong thuong cho nu
            '5: Data sequence
            '6: Test Date
            'Xet tung Test
            Select Case Get_Account_XT1800()
                Case "1" 'Usual
                    If PatientResult.SpecimenID(2).Trim.Length <= 4 Then
                        With PatientResult.ResultArray(0).DateTestCompleted
                            pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(2).Trim)
                        End With
                    Else
                        pBarcode = PatientResult.SpecimenID(2).Trim
                    End If
                    Exit Select
                Case "3" 'Xay Dung
                    If PatientResult.PID.Trim.Length <= 4 Then
                        With PatientResult.ResultArray(0).DateTestCompleted
                            If PatientResult.PID.Trim = "0" Then
                                Exit Sub
                            End If
                            pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.PID.Trim)
                        End With
                    Else
                        pBarcode = PatientResult.PID.Trim
                    End If
                    Exit Select
                Case Else 'Usual
                    If PatientResult.SpecimenID(2).Trim.Length <= 4 Then
                        With PatientResult.ResultArray(0).DateTestCompleted
                            pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(2).Trim)
                        End With
                    Else
                        pBarcode = PatientResult.SpecimenID(2).Trim
                    End If
                    Exit Select
            End Select

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua
            For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                Me.tblParaName = Me.spGetParaInfor(PortNum)
                pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                            vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                            Try
                                If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                    Me.vResult(1, iCount) = (Val(Me.vResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                                End If
                            Catch ex As Exception
                            End Try
                            vResult(1, iCount) = Me.spGetDataToView(vResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                        vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    ' cap nhat vao CSDL
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next iCount
        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\XTCAException.txt", strInput(dem))
                f.AppendAllText("C:\LOG\XTCAException.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\XTCAException.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf)
        End Try
    End Sub
    Sub spxS1000i(ByVal strInput() As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)

        Dim prvResult(6, 0) As String

        Dim clsControl As New clsControls
        Dim clsXS1000i As New xS1000i
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New xS1000i.Patient()
        Dim dem As Integer
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT.log")
        Try

            clsXS1000i.ResultDataParse(strInput, PatientResult)
            Try
                If Not Directory.Exists("C:\LOG\XS1000I") Then Directory.CreateDirectory("C:\LOG\XS1000I")

                For dem = 0 To strInput.GetLength(0) - 1
                    f.AppendAllText("C:\LOG\XS1000I\XS1000I-" & Now.ToString("dd-MM-yyyy H-mm") & ".txt", strInput(dem) & vbCrLf)
                Next dem
                f.AppendAllText("C:\LOG\XS1000I\XS1000I-" & Now.ToString("dd-MM-yyyy H-mm") & ".txt", Now.ToString & vbCrLf & vbCrLf)

            Catch ex As Exception

            End Try




            ReDim prvResult(6, PatientResult.ResultArray.GetLength(0) - 1)
            '0: Name; 
            '1: Gia(tri)
            '2: Don vi; 
            '3: muc thong thuong cho Nam
            '4: Muc thong thuong cho nu
            '5: Data sequence
            '6: Test Date
            'Xet tung Test
            Select Case Get_Account_XT1800()
                Case "1" 'Usual
                    If PatientResult.SpecimenID(2).Trim.Length <= 4 Then
                        With PatientResult.ResultArray(0).DateTestCompleted
                            pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(2).Trim)
                        End With
                    Else
                        pBarcode = PatientResult.SpecimenID(2).Trim
                    End If
                    Exit Select
                Case "6" 'NHTD
                    If Not String.IsNullOrEmpty(PatientResult.PID.Trim) Then            'Neu ko dien PID thi PID="" do đó chuyen sang dùng SID và chèn chữ S ở trước
                        If PatientResult.PID.Trim.Length <= 4 Then
                            With PatientResult.ResultArray(0).DateTestCompleted
                                'If PatientResult.PID.Trim = "0" Then
                                '    Exit Sub
                                'End If
                                pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.PID.Trim)
                            End With
                        Else
                            pBarcode = PatientResult.PID.Trim
                        End If
                    Else
                        Try

                            Dim st As String = PatientResult.SpecimenID(2).Trim
                            If (st.Length > 3) Then
                                st = st.Substring(st.Length - 3)

                            End If
                            pBarcode = PatientResult.ResultArray(0).DateTestCompleted.Substring(2, 6) & Me.spGetintOder(pTestTypeID) & "S" & st.PadLeft(3, "0")

                        Catch ex As Exception
                            pBarcode = PatientResult.ResultArray(0).DateTestCompleted.Substring(2, 6) & Me.spGetintOder(pTestTypeID) & "S000"
                        End Try

                    End If


                    Exit Select

                Case Else 'Usual
                    If PatientResult.SpecimenID(2).Trim.Length <= 4 Then
                        With PatientResult.ResultArray(0).DateTestCompleted
                            pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(2).Trim)
                        End With
                    Else
                        pBarcode = PatientResult.SpecimenID(2).Trim
                    End If
                    Exit Select
            End Select


            ' Xet tung ket qua
            For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                Me.tblParaName = Me.spGetParaInfor(PortNum)
                pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    prvResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    prvResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    prvResult(3, iCount) = ""
                    prvResult(4, iCount) = ""
                    prvResult(5, iCount) = 0
                    prvResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            prvResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                            prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                            Try
                                If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                    prvResult(1, iCount) = (Val(prvResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                                End If
                            Catch ex As Exception
                            End Try
                            prvResult(1, iCount) = Me.spGetDataToView(prvResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            prvResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            prvResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            prvResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            prvResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If prvResult(0, iCount) Is Nothing Then
                        prvResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                        prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        prvResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                        prvResult(6, iCount) = pTestDate
                    End If
                End If

                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    ' Neu da co du lieu nay
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    ' cap nhat vao CSDL
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
            Next iCount

            Dim dt As DataTable = lablinkhelper.Utilities.ConvertToDataTable(prvResult)
            If Not Directory.Exists("C:\LOG\XS1000I\Result") Then Directory.CreateDirectory("C:\LOG\XS1000I\Result")
            lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\XS1000I\Result\XS1000I-" & Now.ToString("dd-MM-yyyy H-mm-s" & "-" & iCount.ToString) & "-" & pBarcode & ".txt")

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID, "C:\LOG\XS1000I")

            For iCount = 0 To prvResult.GetLength(1) - 1
                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    ' Neu da co du lieu nay
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    ' cap nhat vao CSDL
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
                'clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), Test_ID)

                Me.spInsertUpdateResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount), PortNum)
            Next iCount

        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\xSInputException.txt", strInput(dem))
                f.AppendAllText("C:\LOG\xSInputException.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\xSInputException.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf)
        End Try
    End Sub
    Sub spCA500(ByVal strInput() As String, _
                ByVal pTestTypeID As Int16, _
                ByVal PortNum As Int16, _
                ByVal strDeviceName As String _
           )

        Dim prvResult(6, 0) As String

        Dim clsControl As New clsControls
        Dim clsCA500 As New CA500
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New CA500.Patient()
        Dim dem As Integer
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT.log")
        Try
            Try
                If Not Directory.Exists("C:\LOG\CA500") Then Directory.CreateDirectory("C:\LOG\CA500")

                For dem = 0 To strInput.GetLength(0) - 1
                    f.AppendAllText("C:\LOG\CA500\CA500-" & Now.ToString("dd-MM-yyyy H-mm") & ".txt", strInput(dem) & vbCrLf)
                Next dem
                f.AppendAllText("C:\LOG\CA500\CA500-" & Now.ToString("dd-MM-yyyy H-mm") & ".txt", Now.ToString & vbCrLf & vbCrLf)

            Catch ex As Exception

            End Try

            clsCA500.ResultDataParse_CA(strInput, PatientResult)

            ReDim prvResult(6, PatientResult.ResultArray.GetLength(0))
            '0: Name; 
            '1: Gia(tri)
            '2: Don vi; 
            '3: muc thong thuong cho Nam
            '4: Muc thong thuong cho nu
            '5: Data sequence
            '6: Test Date
            'Xet tung Test

            If PatientResult.SpecimenID(2).Trim.Length <= 4 Then
                With PatientResult.ResultArray(0).DateTestCompleted
                    pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(2).Trim)
                End With
            Else
                pBarcode = PatientResult.SpecimenID(2).Trim
            End If

            ' Xet tung ket qua
            For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                Me.tblParaName = Me.spGetParaInfor(PortNum)
                pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    prvResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    prvResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    prvResult(3, iCount) = ""
                    prvResult(4, iCount) = ""
                    prvResult(5, iCount) = 0
                    prvResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If PatientResult.ResultArray(iCount).TestCode.Trim = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            prvResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                            prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                            Try
                                If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                    prvResult(1, iCount) = (Val(prvResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                                End If
                            Catch ex As Exception
                            End Try
                            prvResult(1, iCount) = Me.spGetDataToView(prvResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            prvResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            prvResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            prvResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            prvResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            prvResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If prvResult(0, iCount) Is Nothing Then
                        prvResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                        prvResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        prvResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                        prvResult(3, iCount) = ""
                        prvResult(4, iCount) = ""
                        prvResult(5, iCount) = 0
                        prvResult(6, iCount) = pTestDate
                    End If
                End If
                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    ' Neu da co du lieu nay
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    ' cap nhat vao CSDL
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
                'clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), Test_ID)
            Next iCount

            Dim dt As DataTable = lablinkhelper.Utilities.ConvertToDataTable(prvResult)
            If Not Directory.Exists("C:\LOG\CA500\Result") Then Directory.CreateDirectory("C:\LOG\CA500\Result")
            lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\CA500\Result\CA500-" & Now.ToString("dd-MM-yyyy H-mm-s") & "-" & pBarcode & ".txt")

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

            For iCount = 0 To prvResult.GetLength(1) - 1
                'If spCheckBarcode(pBarcode, prvResult(0, iCount), pTestDetailID) Then
                '    ' Neu da co du lieu nay
                '    Me.SpUpdateTestResult(pTestDetailID, pBarcode, prvResult(1, iCount))
                'Else
                '    ' cap nhat vao CSDL
                '    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount))
                'End If
                'clsControl.SpUpdateRegTestList(pBarcode, prvResult(0, iCount), Test_ID)

                Me.spInsertUpdateResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, prvResult(5, iCount), pBarcode, prvResult(0, iCount), prvResult(1, iCount), prvResult(2, iCount), prvResult(3, iCount), prvResult(4, iCount), PortNum)
            Next iCount
        Catch ex As Exception
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\CAException.txt", strInput(dem))
                f.AppendAllText("C:\LOG\CAException.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\CAException.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf & vbCrLf)
        End Try
    End Sub

    Sub spRUBY(ByVal strInput() As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsRUBY As New RUBY
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim dem As Integer = 0
        Dim PatientResult As New RUBY.Patient()
        Dim f As File
        Dim TempstrInput As String
        Try
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\RubyInput.txt", strInput(dem))
                f.AppendAllText("C:\LOG\RubyInput.txt", vbCrLf)
            Next dem
            'f.AppendAllText("C:\LOG\RubyInput.txt", Now.ToString & vbCrLf & vbCrLf)
            'f.WriteAllLines("C:\LOG\RubyInput" & Now.Day & Now.Month & Now.Year & Now.Hour & Now.Minute & Now.Second & ".txt", strInput)
            'f.AppendAllText("C:\LOG\RubyInput" & Now.Day & Now.Month & Now.Year & Now.Hour & Now.Minute & Now.Second & ".txt", vbCrLf)
            clsRUBY.ResultDataParse(strInput, PatientResult)
            ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
            '0: Name; 
            '1: Gia(tri)
            '2: Don vi; 
            '3: muc thong thuong cho Nam
            '4: Muc thong thuong cho nu
            '5: Data sequence
            '6: Test Date
            'Xet tung Test
            If PatientResult.SpecimenID(0).Trim.Length <= 4 Then
                With PatientResult.ResultArray(0).DateTestCompleted
                    pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SpecimenID(0).Trim)
                End With
            Else
                pBarcode = PatientResult.SpecimenID(0).Trim
            End If

            Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
            ' Xet tung ket qua

            For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
                Me.tblParaName = Me.spGetParaInfor(PortNum)
                pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                Else
                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                        If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                            vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                            vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                            Try
                                If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                    Me.vResult(1, iCount) = (Val(Me.vResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                                End If
                            Catch ex As Exception

                            End Try
                            vResult(1, iCount) = Me.spGetDataToView(vResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                            vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                            vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                            vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                            vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                            vResult(6, iCount) = pTestDate
                            Exit For
                        End If
                    Next iPos

                    If vResult(0, iCount) Is Nothing Then
                        vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                        vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                        vResult(3, iCount) = ""
                        vResult(4, iCount) = ""
                        vResult(5, iCount) = 0
                        vResult(6, iCount) = pTestDate
                    End If
                End If
                If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                    ' Neu da co du lieu nay
                    Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
                Else
                    ' cap nhat vao CSDL
                    Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
                End If
                clsControl.SpUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)
            Next iCount
        Catch ex As Exception
            'f.WriteAllLines("C:\LOG\RubyExceptionInput" & Now.Day & Now.Month & Now.Year & Now.Hour & Now.Minute & Now.Second & ".txt", strInput)
            'f.AppendAllText("C:\LOG\RubyExceptionInput" & Now.Day & Now.Month & Now.Year & Now.Hour & Now.Minute & Now.Second & ".txt", vbCrLf & ex.ToString & vbCrLf)
            For dem = 0 To strInput.GetLength(0) - 1
                f.AppendAllText("C:\LOG\RubyException.txt", strInput(dem))
                f.AppendAllText("C:\LOG\RubyException.txt", vbCrLf)
            Next dem
            f.AppendAllText("C:\LOG\RubyException.txt", vbCrLf & ex.ToString & vbCrLf & Now.ToString & vbCrLf & vbCrLf)
        End Try
    End Sub

    Sub spGEMPLUS3000(ByVal strInput() As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsGEMPLUS3000 As New GEMPLUS3000
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New GEMPLUS3000.Patient()
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT1800i_Records.log")
        clsGEMPLUS3000.ResultDataParse(strInput, PatientResult)
        ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
        '0: Name; 
        '1: Gia(tri)
        '2: Don vi; 
        '3: muc thong thuong cho Nam
        '4: Muc thong thuong cho nu
        '5: Data sequence
        '6: Test Date
        'Xet tung Test

        If PatientResult.SID.Trim.Length <= 4 Then
            With PatientResult.ResultArray(0).DateTestCompleted
                pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SID.Trim)
            End With
        Else
            pBarcode = PatientResult.SID.Trim
        End If

        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
        ' Xet tung ket qua
        For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
            Me.tblParaName = Me.spGetParaInfor(PortNum)
            pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                vResult(3, iCount) = ""
                vResult(4, iCount) = ""
                vResult(5, iCount) = 0
                vResult(6, iCount) = pTestDate
            Else
                For iPos = 0 To Me.tblParaName.Rows.Count - 1
                    If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                        vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                        vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        Try
                            If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                Me.vResult(1, iCount) = (Val(Me.vResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                            End If
                        Catch ex As Exception

                        End Try


                        vResult(1, iCount) = Me.spGetDataToView(vResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                        vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                        vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                        vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                        vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                        vResult(6, iCount) = pTestDate
                        Exit For
                    End If
                Next iPos

                If vResult(0, iCount) Is Nothing Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                End If
            End If
            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                ' Neu da co du lieu nay
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))

            Else
                ' cap nhat vao CSDL
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
            End If

            'clsControl.spUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)

        Next iCount
    End Sub

    Sub spNOVA(ByVal strInput() As String, _
                     ByVal pTestTypeID As Int16, _
                     ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsNOVA As New NOVA
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New NOVA.Patient()
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT1800i_Records.log")
        clsNOVA.ResultDataParse(strInput, PatientResult)
        ReDim vResult(6, PatientResult.ResultArray.GetLength(0))
        '0: Name; 
        '1: Gia(tri)
        '2: Don vi; 
        '3: muc thong thuong cho Nam
        '4: Muc thong thuong cho nu
        '5: Data sequence
        '6: Test Date
        'Xet tung Test

        If PatientResult.SID.Trim.Length <= 4 Then
            With PatientResult.ResultArray(0).DateTestCompleted
                pBarcode = .Substring(2, 6) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SID.Trim)
            End With
        Else
            pBarcode = PatientResult.SID.Trim
        End If

        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
        ' Xet tung ket qua
        For iCount = 0 To PatientResult.ResultArray.GetLength(0) - 1
            Me.tblParaName = Me.spGetParaInfor(PortNum)
            pTestDate = PatientResult.ResultArray(iCount).DateTestCompleted.Substring(6, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(4, 2) & "/" & PatientResult.ResultArray(iCount).DateTestCompleted.Substring(0, 4)
            If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                vResult(3, iCount) = ""
                vResult(4, iCount) = ""
                vResult(5, iCount) = 0
                vResult(6, iCount) = pTestDate
            Else
                For iPos = 0 To Me.tblParaName.Rows.Count - 1
                    If PatientResult.ResultArray(iCount).TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                        vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                        vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                        Try
                            If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                                Me.vResult(1, iCount) = (Val(Me.vResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                            End If
                        Catch ex As Exception

                        End Try


                        vResult(1, iCount) = Me.spGetDataToView(vResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                        vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                        vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                        vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                        vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                        vResult(6, iCount) = pTestDate
                        Exit For
                    End If
                Next iPos

                If vResult(0, iCount) Is Nothing Then
                    vResult(0, iCount) = PatientResult.ResultArray(iCount).TestCode
                    vResult(1, iCount) = PatientResult.ResultArray(iCount).DataValue
                    vResult(2, iCount) = PatientResult.ResultArray(iCount).Unit
                    vResult(3, iCount) = ""
                    vResult(4, iCount) = ""
                    vResult(5, iCount) = 0
                    vResult(6, iCount) = pTestDate
                End If
            End If
            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                ' Neu da co du lieu nay
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))

            Else
                ' cap nhat vao CSDL
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
            End If

            'clsControl.spUpdateRegTestList(pBarcode, vResult(0, iCount), Test_ID)

        Next iCount
    End Sub

    Sub spExpressplus(ByVal strInput As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)
        Dim clsexpress As New Expressplus
        Dim strTemp() As String
        Dim strGS() As String
        Dim Test_ID, Patient_ID As Int64
        Dim iCount, iPos, iPatientNumber, iTotal As Int16
        Dim v_Date, pBarCode, strBackUpData As String
        Dim strAll() As String


        ' strInput = "RECV_RSLTSrCUP2REQ2rTYPEPATrPRIONORMALiPID2iPNAMENGUYEN THI QUYAGESEXTS=Creatin54.8247Micromol/lTS=Gluco10.9960mmol/l    HIGHS134"
        'strInput = "RECV_RSLTSrCUP9REQ9rTYPEPATrPRIONORMALiPID12iPNAMEDANG DINH THUYENAGE47SEXNAMTS=GOT19.2725U/L       S333"
        Try
            strBackUpData = strInput
            If clsexpress.CheckACK(strInput) Then

                strInput = strInput.Substring(1)
                strAll = strInput.Split(Chr(2))

                For iPatientNumber = 0 To strAll.GetLength(0) - 1
                    ' Duyet tung ban ghi
                    strInput = strAll(iPatientNumber)

                    strTemp = strInput.Split(Chr(28))

                    strGS = strTemp(5).Split(Chr(29))

                    If strGS(1).Trim <> "" Then
                        ' Neu co barcode thi moi cap nhat
                        'pBarCode = Date.Today.Year.ToString & Format(Date.Today.Month, "0#") & Format(Date.Today.Day, "0#") & strGS(1)
                        pBarCode = Date.Today.Year.ToString.Substring(2) & Format(Date.Today.Month, "0#") & Format(Date.Today.Day, "0#") & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(strGS(1).Trim)

                        v_Date = Format(Date.Today.Day, "0#") & "/" & Format(Date.Today.Month, "0#") & "/" & Date.Today.Year.ToString

                        Me.spGetID(pBarCode, pTestTypeID, Test_ID, Patient_ID)
                        Me.tblParaName = Me.spGetParaInfor(PortNum)

                        iTotal = 0
                        ReDim vResult(5, 20)
                        Dim bTest As Boolean = False

                        For iCount = 0 To strTemp.GetLength(0) - 1
                            If strTemp(iCount).Substring(0, 3) = "TS=" Then
                                strGS = strTemp(iCount).Substring(0, strTemp(iCount).Length - 2).Split(Chr(29))

                                ' Lay thong tin tham so
                                If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                                    vResult(0, iTotal) = strGS(0).Substring(3)
                                    vResult(1, iTotal) = strGS(1)
                                    vResult(2, iTotal) = strGS(2)
                                    vResult(3, iTotal) = ""
                                    vResult(4, iTotal) = ""
                                    vResult(5, iTotal) = 0
                                Else
                                    For iPos = 0 To Me.tblParaName.Rows.Count - 1
                                        If strGS(0).Substring(3) = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                                            vResult(0, iTotal) = Me.tblParaName.Rows(iPos).Item("Data_name")
                                            vResult(1, iTotal) = Me.spGetDataToView(strGS(1), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                                            vResult(2, iTotal) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                                            vResult(3, iTotal) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                                            vResult(4, iTotal) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                                            vResult(5, iTotal) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                                            Exit For

                                        End If
                                    Next

                                    If vResult(0, iTotal) Is Nothing Then
                                        vResult(0, iTotal) = strGS(0).Substring(3)
                                        vResult(1, iTotal) = strGS(1)
                                        vResult(2, iTotal) = strGS(2)
                                        vResult(3, iTotal) = ""
                                        vResult(4, iTotal) = ""
                                        vResult(5, iTotal) = 0
                                    End If
                                End If
                                iTotal = iTotal + 1
                            End If
                        Next

                        Dim pTestDetailID As Int64
                        For iCount = 0 To iTotal - 1
                            If spCheckBarcode(pBarCode, Me.vResult(0, iCount), pTestDetailID) Then
                                ' Neu da co du lieu nay
                                Me.SpUpdateTestResult(pTestDetailID, pBarCode, Me.vResult(1, iCount))
                            Else
                                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, " ", v_Date, vResult(5, iCount), pBarCode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
                            End If

                        Next
                    End If
                Next

            Else
            End If
        Catch ex As Exception
            Dim f As File
            f.WriteAllText("C:\LOG\abc.txt", strBackUpData)

        End Try

    End Sub

    Sub spACS180(ByVal strInput As String, _
                      ByVal pTestTypeID As Int16, _
                      ByVal PortNum As Int16)
        Dim cls As New ACS180
        Dim KQ As ACS180.Ket_qua()
        Dim strDauVao() As String
        Dim pParaNameTemp, pTestDate, pBarcode As String
        Dim iCount, iRelTest, iTotal, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64

        Try
            strDauVao = strInput.Split(Chr(10))

            KQ = cls.Xu_ly(strDauVao)

            For iCount = 0 To KQ.GetLength(0) - 1

                Me.tblParaName = Me.spGetParaInfor(PortNum)
                ReDim vResult(6, 20)
                iTotal = 0

                For iRelTest = 0 To KQ(iCount).Ket_qua.GetLength(0) - 1

                    pTestDate = KQ(iCount).ngay_xet_nghiem(iRelTest).Substring(0, 10)
                    'pBarcode = pTestDate.Substring(6, 4) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & KQ(iCount).PID
                    pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(KQ(iCount).PID)


                    pParaNameTemp = KQ(iCount).Tenxetnghiem(iRelTest)
                    Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

                    If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
                        vResult(0, iTotal) = pParaNameTemp
                        vResult(1, iTotal) = KQ(iCount).Ket_qua(iRelTest)
                        vResult(2, iTotal) = KQ(iCount).Don_vi(iRelTest)
                        vResult(3, iTotal) = ""
                        vResult(4, iTotal) = ""
                        vResult(5, iTotal) = 0
                        vResult(6, iTotal) = KQ(iCount).ngay_xet_nghiem(iRelTest).Substring(0, 10)
                    Else
                        For iPos = 0 To Me.tblParaName.Rows.Count - 1

                            If pParaNameTemp = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                                vResult(0, iTotal) = Me.tblParaName.Rows(iPos).Item("Data_name")
                                vResult(1, iTotal) = Me.spGetDataToView(KQ(iCount).Ket_qua(iRelTest), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                                vResult(2, iTotal) = KQ(iCount).Don_vi(iRelTest)
                                vResult(3, iTotal) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                                vResult(4, iTotal) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                                vResult(5, iTotal) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                                vResult(6, iTotal) = KQ(iCount).ngay_xet_nghiem(iRelTest).Substring(0, 10)
                                Exit For
                            End If
                        Next

                        If vResult(0, iTotal) Is Nothing Then
                            vResult(0, iTotal) = pParaNameTemp
                            vResult(1, iTotal) = KQ(iCount).Ket_qua(iRelTest)
                            vResult(2, iTotal) = KQ(iCount).Don_vi(iRelTest)
                            vResult(3, iTotal) = ""
                            vResult(4, iTotal) = ""
                            vResult(5, iTotal) = 0
                            vResult(6, iTotal) = KQ(iCount).ngay_xet_nghiem(iRelTest).Substring(0, 10)
                        End If
                    End If
                    iTotal = iTotal + 1

                Next

                ' cap nhat vao CSDL
                For iRelTest = 0 To iTotal - 1
                    If spCheckBarcode(pBarcode, vResult(0, iRelTest), pTestDetailID) Then
                        ' Neu da co du lieu nay
                        Me.SpUpdateTestResult(pTestDetailID, pBarcode, vResult(1, iRelTest))
                    Else
                        Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, KQ(iCount).Khay_cup, vResult(6, iRelTest), vResult(5, iRelTest), pBarcode, vResult(0, iRelTest), vResult(1, iRelTest), vResult(2, iRelTest), vResult(3, iRelTest), vResult(4, iRelTest))
                    End If
                Next
            Next

        Catch ex As Exception
            Dim f As File
            f.WriteAllText("C:\LOG\acs.txt", strInput)
        End Try

    End Sub

    Sub spCOBAS400(ByVal strInput As String, _
                         ByVal pTestTypeID As Int16, _
                         ByVal PortNum As Int16)
        Dim clsControl As New clsControls
        Dim clsCB400 As New COBAS400
        Dim pBarcode, pTestDate As String
        Dim iCount, iPos As Int16
        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim PatientResult As New COBAS400.Patient()
        Dim f As File
        'strInput = f.ReadAllLines("C:\LOG\XT1800i_Records.log")
        If Not clsCB400.ResultParse(strInput, PatientResult) Then
            Exit Sub
        End If
        ReDim vResult(6, 0)
        '0: Name; 
        '1: Gia(tri)
        '2: Don vi; 
        '3: muc thong thuong cho Nam
        '4: Muc thong thuong cho nu
        '5: Data sequence
        '6: Test Date
        'Xet tung Test

        If PatientResult.SID.Trim.Length <= 4 Then
            With PatientResult.DateTestCompleted
                pBarcode = .Substring(8, 2) & .Substring(3, 2) & .Substring(0, 2) & Me.spGetintOder(pTestTypeID) & Me.spGetBarcode(PatientResult.SID)
            End With
        Else
            pBarcode = PatientResult.SID.Trim
        End If

        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)
        ' Xet tung ket qua
        Me.tblParaName = Me.spGetParaInfor(PortNum)
        pTestDate = PatientResult.DateTestCompleted
        If Me.tblParaName Is Nothing Or Me.tblParaName.Rows.Count = 0 Then
            vResult(0, iCount) = PatientResult.TestCode
            vResult(1, iCount) = PatientResult.DataValue
            vResult(2, iCount) = ""
            vResult(3, iCount) = ""
            vResult(4, iCount) = ""
            vResult(5, iCount) = 0
            vResult(6, iCount) = pTestDate
        Else
            For iPos = 0 To Me.tblParaName.Rows.Count - 1
                If PatientResult.TestCode = Me.tblParaName.Rows(iPos).Item("Alias_name") Then
                    vResult(0, iCount) = Me.tblParaName.Rows(iPos).Item("Data_name")

                    vResult(1, iCount) = PatientResult.DataValue
                    Try
                        If Me.tblParaName.Rows(iPos).Item("Data_Type") <> "" Then
                            Me.vResult(1, iCount) = (Val(Me.vResult(1, iCount)) * Me.tblParaName.Rows(iPos).Item("Data_Type")).ToString
                        End If
                    Catch ex As Exception

                    End Try


                    vResult(1, iCount) = Me.spGetDataToView(vResult(1, iCount), Me.tblParaName.Rows(iPos).Item("Data_Point"))
                    vResult(2, iCount) = Me.tblParaName.Rows(iPos).Item("Measure_Unit")
                    vResult(3, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_Level")
                    vResult(4, iCount) = Me.tblParaName.Rows(iPos).Item("Normal_LevelW")
                    vResult(5, iCount) = Me.tblParaName.Rows(iPos).Item("Data_Sequence")
                    vResult(6, iCount) = pTestDate
                    Exit For
                End If
            Next iPos

            If vResult(0, iCount) Is Nothing Then
                vResult(0, iCount) = PatientResult.TestCode
                vResult(1, iCount) = PatientResult.DataValue
                vResult(2, iCount) = ""
                vResult(3, iCount) = ""
                vResult(4, iCount) = ""
                vResult(5, iCount) = 0
                vResult(6, iCount) = pTestDate
            End If
        End If
        If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
            ' Neu da co du lieu nay
            Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))

        Else
            ' cap nhat vao CSDL
            Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, "", pTestDate, vResult(5, iCount), pBarcode, vResult(0, iCount), vResult(1, iCount), vResult(2, iCount), vResult(3, iCount), vResult(4, iCount))
        End If
    End Sub

    Sub spExcell(ByVal strInput As String, _
                              ByVal pTestTypeID As Int16, _
                              ByVal PortNum As Int16)

        Dim Test_ID, Patient_ID, pTestDetailID As Int64
        Dim f As File

        'strInput = f.ReadAllText("C:\LOG\excel18.log")

        ReDim vResult(6, 0)
        '0: Name; 
        '1: Gia(tri)
        '2: Don vi; 
        '3: muc thong thuong cho Nam
        '4: Muc thong thuong cho nu
        '5: Data sequence
        '6: Test Date
        'Xet tung Test

        Dim iCount, iPara, iQC, iSex As Int16
        Dim pTestDate, pTestSeq, pBarcode As String

        pTestSeq = strInput.Substring(1, 3)
        pBarcode = Me.spGetBarcode(strInput.Substring(4, 9).Trim)
        If pBarcode = "0000" Then
            Exit Sub
        End If

        iSex = strInput.Substring(54, 1)
        pTestDate = strInput.Substring(102, 10).Trim
        pTestDate = pTestDate.Substring(0, 2) & "/" & pTestDate.Substring(3, 2) & "/" & "20" & pTestDate.Substring(8, 2)

        pBarcode = pTestDate.Substring(8, 2) & pTestDate.Substring(3, 2) & pTestDate.Substring(0, 2) & Me.spGetintOder(pTestTypeID) & pBarcode

        If Me.spCheckSequence(pTestSeq, pTestDate) Then
            Exit Sub
        End If

        tblParaName = Me.spGetParaInfor(PortNum)
        ReDim Me.vResult(5, 17)

        iQC = 0 ' Dung de kiem tra khi khoi dong

        Me.vResult(0, 0) = "WBC"
        Me.vResult(1, 0) = strInput.Substring(123, 4).Trim

        Me.vResult(0, 15) = "#LYM"
        Me.vResult(1, 15) = strInput.Substring(128, 4).Trim

        Me.vResult(0, 16) = "#MID"
        Me.vResult(1, 16) = strInput.Substring(133, 4).Trim

        Me.vResult(0, 17) = "#GRA"
        Me.vResult(1, 17) = strInput.Substring(138, 4).Trim

        Me.vResult(0, 12) = "%LYM"
        Me.vResult(1, 12) = strInput.Substring(143, 4).Trim

        Me.vResult(0, 13) = "%MID"
        Me.vResult(1, 13) = strInput.Substring(148, 4).Trim

        Me.vResult(0, 14) = "%GRA"
        Me.vResult(1, 14) = strInput.Substring(153, 4).Trim

        Me.vResult(0, 1) = "RBC"
        Me.vResult(1, 1) = strInput.Substring(158, 5).Trim

        Me.vResult(0, 2) = "HGB"
        Me.vResult(1, 2) = strInput.Substring(163, 5).Trim

        Me.vResult(0, 3) = "HCT"
        Me.vResult(1, 3) = strInput.Substring(168, 5).Trim

        Me.vResult(0, 4) = "MCV"
        Me.vResult(1, 4) = strInput.Substring(173, 5).Trim

        Me.vResult(0, 5) = "MCH"
        Me.vResult(1, 5) = strInput.Substring(178, 5).Trim

        Me.vResult(0, 6) = "MCHC"
        Me.vResult(1, 6) = strInput.Substring(183, 5).Trim

        Me.vResult(0, 7) = "RDW"
        Me.vResult(1, 7) = strInput.Substring(188, 5).Trim

        Me.vResult(0, 8) = "PLT"
        Me.vResult(1, 8) = strInput.Substring(193, 5).Trim

        Me.vResult(0, 9) = "MPV"
        Me.vResult(1, 9) = strInput.Substring(198, 5).Trim

        Me.vResult(0, 10) = "PCT"
        Me.vResult(1, 10) = strInput.Substring(203, 5).Trim

        Me.vResult(0, 11) = "PDW"
        Me.vResult(1, 11) = strInput.Substring(208, 5).Trim

        ' Kiem tra QC
        For iCount = 0 To Me.vResult.GetLength(1) - 1
            If Me.spGetDataToView(Me.vResult(1, iCount), 0) = "0" Then
                iQC = iQC + 1
            End If
        Next

        If iQC >= 10 Then
            ' La QC
            Exit Sub
        End If

        If Me.tblParaName Is Nothing Then
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                Me.vResult(2, iCount) = ""
                Me.vResult(3, iCount) = ""
                Me.vResult(4, iCount) = ""
                Me.vResult(5, iCount) = 0
            Next
        Else
            For iCount = 0 To Me.vResult.GetLength(1) - 1
                For iPara = 0 To Me.tblParaName.Rows.Count - 1
                    If Me.tblParaName.Rows(iPara).Item("Alias_name") = Me.vResult(0, iCount) Then
                        Me.vResult(0, iCount) = Me.tblParaName.Rows(iPara).Item("Data_name")
                        Me.vResult(1, iCount) = Me.spGetDataToView(Me.vResult(1, iCount), Me.tblParaName.Rows(iPara).Item("Data_Point"))
                        Me.vResult(2, iCount) = Me.tblParaName.Rows(iPara).Item("Measure_Unit")
                        Me.vResult(3, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_Level")
                        Me.vResult(4, iCount) = Me.tblParaName.Rows(iPara).Item("Normal_LevelW")
                        Me.vResult(5, iCount) = Me.tblParaName.Rows(iPara).Item("Data_Sequence")
                    End If
                Next

            Next

        End If

        Me.spGetID(pBarcode, pTestTypeID, Test_ID, Patient_ID)

        For iCount = 0 To Me.vResult.GetLength(1) - 1
            If spCheckBarcode(pBarcode, Me.vResult(0, iCount), pTestDetailID) Then
                ' Neu da co du lieu nay
                Me.SpUpdateTestResult(pTestDetailID, pBarcode, Me.vResult(1, iCount))
            Else
                Me.SpInsertResult(Test_ID, Patient_ID, pTestTypeID, pTestSeq, pTestDate, Me.vResult(5, iCount), pBarcode, Me.vResult(0, iCount), Me.vResult(1, iCount), Me.vResult(2, iCount), Me.vResult(3, iCount), Me.vResult(4, iCount))
            End If
        Next

    End Sub

    'Các hàm xử lý dùng chung

    ' Đầu vào là hai mảng 
    Private Shared Function SpInsertResult(ByVal pTestID As Int64, _
                            ByVal pPatientID As Int64, _
                            ByVal pTestTypeID As Int16, _
                            ByVal pTestSeq As String, _
                            ByVal pTestDate As String, _
                            ByVal pDataSeq As Int16, _
                            ByVal pBarcode As String, _
                            ByVal pParaName As String, _
                            ByVal pTestResult As String, _
                            ByVal pMeasureUnit As String, _
                            ByVal pNormalLevel As String, _
                            ByVal pNormalLevelW As String) As Boolean

        Dim cmd As New SqlCommand
        Dim methodConnection As SqlConnection
        Try
            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spInsertResult" & vbCrLf)
                Return False
            End Try

            Dim dt As New DataTable
            dt.Columns.Add("CommandText")
            dt.Columns.Add("pTest_ID")
            dt.Columns.Add("pPatient_ID")
            dt.Columns.Add("pTestType_ID")
            dt.Columns.Add("pTestSeq")
            dt.Columns.Add("pTestDate")
            dt.Columns.Add("pDataSeq")
            dt.Columns.Add("pBarcode")
            dt.Columns.Add("pParaName")
            dt.Columns.Add("pTestResult")
            dt.Columns.Add("pMeasureUnit")
            dt.Columns.Add("pNormalLevel")
            dt.Columns.Add("pNormalLevelW")
            Dim dr As DataRow = dt.NewRow

            With cmd
                .Connection = methodConnection
                .CommandText = "spInsertTestResult"
                .CommandType = CommandType.StoredProcedure
                dr(0) = "spInsertTestResult"

                .Parameters.Add("pTest_ID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pTest_ID").Value = pTestID
                dr(1) = pTestID.ToString

                .Parameters.Add("pPatient_ID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pPatient_ID").Value = pPatientID
                dr(2) = pPatientID.ToString

                .Parameters.Add("pTestType_ID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pTestType_ID").Value = pTestTypeID
                dr(3) = pTestTypeID.ToString


                .Parameters.Add("pTestSeq", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestSeq").Value = " " 'pTestSeq
                dr(4) = .Parameters("pTestSeq").Value.ToString


                .Parameters.Add("pTestDate", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pTestDate").Value = pTestDate
                dr(5) = pTestDate.ToString

                .Parameters.Add("pDataSeq", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pDataSeq").Value = pDataSeq
                dr(6) = pDataSeq.ToString


                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                dr(7) = pBarcode.ToString

                .Parameters.Add("pParaName", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pParaName").Value = pParaName
                dr(8) = pParaName.ToString


                .Parameters.Add("pTestResult", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestResult").Value = pTestResult
                dr(9) = pTestResult.ToString

                .Parameters.Add("pMeasureUnit", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pMeasureUnit").Value = IIf(pMeasureUnit Is Nothing, " ", pMeasureUnit)
                dr(10) = .Parameters("pMeasureUnit").Value.ToString

                .Parameters.Add("pNormalLevel", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevel").Value = IIf(pNormalLevel Is Nothing, " ", pNormalLevel)
                dr(11) = .Parameters("pNormalLevel").Value.ToString


                .Parameters.Add("pNormalLevelW", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevelW").Value = IIf(pNormalLevelW Is Nothing, " ", pNormalLevelW)
                dr(12) = .Parameters("pNormalLevelW").Value.ToString

                dt.Rows.Add(dr)
                dt.NewRow()
                dt.NewRow()

                lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\" & Now.ToString("dd-MM-yyyy hh-mm") & "-" & pBarcode & ".txt", True)


                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spInsertResult - - - Lỗi Khác" & vbCrLf & ex.Message & vbCrLf _
                               & pTestDate & vbCrLf _
                               & pBarcode & vbCrLf _
                               & pParaName & vbCrLf _
                               & pTestResult & vbCrLf)
            Return False
        Finally
            methodConnection.Dispose()
            cmd.Dispose()
        End Try

    End Function

    Private Shared Function spInsertUpdateResult(ByVal pTestID As Int64, _
                            ByVal pPatientID As Int64, _
                            ByVal pTestTypeID As Int16, _
                            ByVal pTestSeq As String, _
                            ByVal pTestDate As String, _
                            ByVal pDataSeq As Int16, _
                            ByVal pBarcode As String, _
                            ByVal pParaName As String, _
                            ByVal pTestResult As String, _
                            ByVal pMeasureUnit As String, _
                            ByVal pNormalLevel As String, _
                            ByVal pNormalLevelW As String, _
                            ByVal pPortNum As Integer) As Boolean

        Dim cmd As New SqlCommand
        Dim methodConnection As SqlConnection
        Try
            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spInsertResult" & vbCrLf)
                Return False
            End Try

            Dim dt As New DataTable
            dt.Columns.Add("CommandText")
            dt.Columns.Add("pTest_ID")
            dt.Columns.Add("pPatient_ID")
            dt.Columns.Add("pTestType_ID")
            dt.Columns.Add("pTestSeq")
            dt.Columns.Add("pTestDate")
            dt.Columns.Add("pDataSeq")
            dt.Columns.Add("pBarcode")
            dt.Columns.Add("pParaName")
            dt.Columns.Add("pTestResult")
            dt.Columns.Add("pMeasureUnit")
            dt.Columns.Add("pNormalLevel")
            dt.Columns.Add("pNormalLevelW")
            dt.Columns.Add("pPortNum")
            Dim dr As DataRow = dt.NewRow

            With cmd
                .Connection = methodConnection
                .CommandText = "spInsertUpdateTestResult"
                .CommandType = CommandType.StoredProcedure
                dr(0) = "spInsertUpdateTestResult"

                .Parameters.Add("pTest_ID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pTest_ID").Value = pTestID
                dr(1) = pTestID.ToString

                .Parameters.Add("pPatient_ID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pPatient_ID").Value = pPatientID
                dr(2) = pPatientID.ToString

                .Parameters.Add("pTestType_ID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pTestType_ID").Value = pTestTypeID
                dr(3) = pTestTypeID.ToString


                .Parameters.Add("pTestSeq", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestSeq").Value = " " 'pTestSeq
                dr(4) = .Parameters("pTestSeq").Value.ToString


                .Parameters.Add("pTestDate", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pTestDate").Value = pTestDate
                dr(5) = pTestDate.ToString

                .Parameters.Add("pDataSeq", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pDataSeq").Value = pDataSeq
                dr(6) = pDataSeq.ToString


                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                dr(7) = pBarcode.ToString

                .Parameters.Add("pParaName", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pParaName").Value = pParaName
                dr(8) = pParaName.ToString


                .Parameters.Add("pTestResult", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestResult").Value = pTestResult
                dr(9) = pTestResult.ToString

                .Parameters.Add("pMeasureUnit", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pMeasureUnit").Value = IIf(pMeasureUnit Is Nothing, " ", pMeasureUnit)
                dr(10) = .Parameters("pMeasureUnit").Value.ToString

                .Parameters.Add("pNormalLevel", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevel").Value = IIf(pNormalLevel Is Nothing, " ", pNormalLevel)
                dr(11) = .Parameters("pNormalLevel").Value.ToString


                .Parameters.Add("pNormalLevelW", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevelW").Value = IIf(pNormalLevelW Is Nothing, " ", pNormalLevelW)
                dr(12) = .Parameters("pNormalLevelW").Value.ToString

                .Parameters.Add("pPortNum", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortNum").Value = pPortNum
                dr(13) = .Parameters("pPortNum").Value.ToString

                dt.Rows.Add(dr)
                dt.NewRow()
                dt.NewRow()

                lablinkhelper.Utilities.WriteDataTableToFile(dt, "C:\LOG\" & Now.ToString("dd-MM-yyyy hh-mm") & "-" & pBarcode & ".txt", True)


                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spInsertUpdateTestResult - - - Lỗi Khác" & vbCrLf & ex.Message & vbCrLf _
                               & pTestDate & vbCrLf _
                               & pBarcode & vbCrLf _
                               & pParaName & vbCrLf _
                               & pTestResult & vbCrLf)
            Return False
        Finally
            methodConnection.Dispose()
            cmd.Dispose()
        End Try

    End Function

    Private Shared Function SpUpdateTestResult(ByVal pTestDetailID As Int64, _
                                 ByVal pBarcode As String, _
                                 ByVal pNewResult As String) As Boolean
        Dim cmd As New SqlCommand
        Dim methodConnection As SqlConnection
        Try
            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spUpdateTestResult" & vbCrLf)
                Return False
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spUpdateTestResult"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pTestDetail_ID", SqlDbType.BigInt, 10).Direction = ParameterDirection.Input
                .Parameters("pTestDetail_ID").Value = pTestDetailID
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pTestResult", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pTestResult").Value = pNewResult
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spUpdateTestResult - - Lỗi khác" & vbCrLf)
            Return False
        Finally
            methodConnection.Dispose()
            cmd.Dispose()
        End Try

    End Function

    ' Kiem tra xem Seq nay da co trong he thong chua
    Function spCheckSequence(ByVal pTestSeq As String, _
                             ByVal pTestDate As String) As Boolean
        Try
            Dim cmd As SqlCommand = New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spCheckSequence" & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spCheckTestSeq"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pTestSeq", SqlDbType.NVarChar, 10).Direction = ParameterDirection.Input
                .Parameters("pTestSeq").Value = pTestSeq
                .Parameters.Add("pTestDate", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pTestDate").Value = pTestDate
                .Parameters.Add("pCount", SqlDbType.Int).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            If cmd.Parameters("pCount").Value > 0 Then
                cmd.Dispose()
                Return True
            Else
                cmd.Dispose()
                Return False
            End If
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spCheckSequence - - Lỗi Khác" & vbCrLf)
        End Try
    End Function

    ' Kiem tra xem Patient_ID nay da co chua
    Function spCheckBarcode(ByVal pBarcode As String, ByVal pParaName As String, ByRef pDetailID As Int64) As Boolean

        Dim cmd As New SqlCommand
        Dim methodConnection As SqlConnection
        Try
            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spCheckBarcode" & vbCrLf)
                Return False
            End Try


            With cmd
                .Connection = methodConnection
                .CommandText = "spCheckBarcode"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pParaName", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pParaName").Value = pParaName
                .Parameters.Add("pReSult", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pTestDetailID", SqlDbType.BigInt).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With

            If cmd.Parameters("pReSult").Value > 0 Then
                pDetailID = cmd.Parameters("pTestDetailID").Value
                cmd.Dispose()
                Return True
            Else
                pDetailID = 0
                cmd.Dispose()
                Return False
            End If
        Catch ex As Exception
            pDetailID = 0
            pParaName = "!@#$%^"
            cmd.Dispose()
            Return False
        Finally
            methodConnection.Dispose()
            cmd.Dispose()
        End Try
        'pDetailID = 0
    End Function

    Public Function GetFieldValue(ByVal pv_sTableName As String, ByVal pv_sFieldName As String, ByVal pv_sCondition As String) As String
        'Dim fv_FieldName As String
        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spCheckBarcode" & vbCrLf)
            End Try
            Dim cmd As New SqlCommand
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT " & pv_sFieldName & " FROM " & pv_sTableName & " WHERE " & pv_sCondition
            cmd.Connection = methodConnection

            Dim fv_da As New SqlDataAdapter(cmd)
            Dim fv_dt As New DataTable

            fv_da.Fill(fv_dt)
            If fv_dt.Rows.Count > 0 Then
                fv_da.Dispose()
                Return fv_dt.Rows(0)(0)
            Else
                fv_da.Dispose()
                fv_dt.Dispose()
                Return ""
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function

    ' Lay thong tin ve thong so: ten, don vi
    Function spGetParaInfor(ByVal pPortID As Int16) As DataTable
        Dim cmd As New SqlCommand
        Dim ds1 As New DataSet
        Dim methodConnection As SqlConnection
        Dim ad As New SqlDataAdapter(cmd)
        Try

            'Dim methodConnection As Object
            'methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            'Try
            '    methodConnection.Open()
            'Catch ex As Exception
            '    File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetParaInfor" & vbCrLf)
            'End Try


            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetParaInfor" & vbCrLf)
            End Try


            With cmd
                .Connection = methodConnection
                .CommandText = "spGetParaInfor"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
            End With

            ad.Fill(ds1)
            Return ds1.Tables(0)
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetParaInfor - - - Lỗi khác" & vbCrLf)
        Finally
            cmd.Dispose()
            ad.Dispose()
            methodConnection.Dispose()
        End Try
    End Function

    Public Function spGetDataToView(ByVal strInput As String, ByVal iDataPoint As Int16) As String
        'Dim pVal As Double
        Dim pVal As String
        Dim iCount As Int16
        If iDataPoint = -10 Then
            Return strInput
        Else
            Try
                strInput = strInput.Trim

                For iCount = 0 To strInput.Length - 1
                    If strInput(iCount) <> "." And Not [Char].IsDigit(strInput, iCount) Then
                        Return strInput
                    End If
                Next

                Do While strInput.Substring(0, 1) = "0" And strInput.Length > 1
                    If strInput.Substring(1, 1) = "0" Then
                        strInput = strInput.Remove(0, 1)
                    Else
                        Exit Do
                    End If
                Loop
                pVal = Math.Round(Val(strInput), iDataPoint).ToString()

                Dim id As Integer = pVal.IndexOf(".")
                If (id < 0) And (iDataPoint > 0) Then
                    pVal = pVal + "."
                    For i As Integer = 0 To iDataPoint - 1
                        pVal = pVal + "0"
                    Next i
                ElseIf (id > 0) And (iDataPoint > 0) Then
                    Dim len As Integer = pVal.Substring(id + 1, pVal.Length - id - 1).Length
                    If len < iDataPoint Then
                        For i As Integer = len To iDataPoint - 1
                            pVal = pVal + "0"
                        Next i
                    End If
               
                End If

                Return pVal
            Catch ex As Exception
                Return strInput
            End Try
        End If
    End Function

    Private Function spBuildFormatString(ByVal iDataPoint As Int16) As String
        Dim result As String = "."
        For i As Integer = 1 To iDataPoint
            result = result & "0"
        Next i
        Return result
    End Function

    Private Function spGetBarcode(ByVal pBarcode As String) As String

        Try
            pBarcode = CInt(pBarcode).ToString
            If pBarcode.Length > 4 Then
                'pBarcode = pBarcode.Substring(pBarcode.Length - 4, 4)
            Else
                pBarcode = pBarcode.PadLeft(4, "0")
            End If
            Return pBarcode
        Catch ex As Exception
            If pBarcode.Length > 4 Then
                pBarcode = pBarcode.Substring(pBarcode.Length - 4, 4)
            Else
                pBarcode = pBarcode.PadLeft(4, "0")
            End If

            Return pBarcode
        End Try

    End Function

    ' Lay thong tin ve ma loai XN de xay dung barcode
    Private Function spGetintOder(ByVal pTestTypeID As Int16) As String

        Dim methodConnection As SqlConnection
        Dim cmd As New SqlCommand
        Dim dt As New DataTable
        Dim ad As SqlDataAdapter
        Try
            methodConnection = lablinkhelper.Utilities.GetDBConnection
            Try
                If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetintOder" & vbCrLf)
                Return String.Empty
            End Try

            With cmd
                .Connection = methodConnection
                .CommandType = CommandType.Text
                .CommandText = "SELECT intOrder From T_TEST_TYPE_LIST WHERE TESTTYPE_ID = " & pTestTypeID.ToString
            End With

            ad = New SqlDataAdapter(cmd)
            ad.Fill(dt)

            If dt.Rows.Count <= 0 Then
                Return "00"
            Else
                Return Format(dt.Rows(0).Item(0), "0#")
            End If
        Catch ex As Exception
            File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetintOder - - - Lỗi khác" & vbCrLf)
        Finally
            methodConnection.Dispose()
            cmd.Dispose()
            dt.Dispose()
            ad.Dispose()
        End Try

    End Function

    Sub spGetID(ByVal pBarcode As String, ByRef pTestID As Int64, ByRef pPatientID As Int64)
        Try
            Dim cmd As New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetID" & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spGetIDInfor"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
            End With
            Dim dt As New DataTable
            Dim ad As New SqlDataAdapter(cmd)
            ad.Fill(dt)
            If dt.Rows.Count > 0 Then
                pTestID = dt.Rows(0).Item("TEST_ID")
                pPatientID = dt.Rows(0).Item("PATIENT_ID")
            Else
                pTestID = 0
                pPatientID = 0
            End If
            cmd.Dispose()
            dt.Dispose()
            ad.Dispose()
        Catch ex As Exception

        End Try
    End Sub

    Sub spGetID(ByVal pBarcode As String, ByVal pTestTypeId As Integer, ByRef pTestId As Int64, ByRef pPatientId As Int64, Optional ByVal logdir As String = "C:\LOG")
        Try

            Dim dt As DataTable = GetDataTable("T_TEST_INFO", "Barcode = '" & pBarcode & "' And TestType_ID = " & pTestTypeId)

            Try
                If Not Directory.Exists(logdir) Then Directory.CreateDirectory(logdir)

            Catch ex As Exception
                logdir = "C:\LOG"
                If Not Directory.Exists(logdir) Then Directory.CreateDirectory(logdir)

            End Try

            'lablinkhelper.Utilities.WriteDataTableToFile(dt, logdir & pBarcode & " - spGetID")

            If dt.Rows.Count > 0 Then
                pTestId = dt.Rows(0).Item("TEST_ID")
                pPatientId = dt.Rows(0).Item("PATIENT_ID")
            Else
                pTestId = 0
                pPatientId = 0
            End If

        Catch ex As Exception
            pTestId = 0
            pPatientId = 0
        End Try
    End Sub

    Sub spGetDeviceID(ByVal pBarcode As String, _
                    ByRef pTestID As Int64, ByRef pPatientID As Int64, ByRef pdeviceId As Int64)
        Try
            Dim cmd As New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spGetDeviceID" & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "GetTestInfor"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pTestTypeId", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pTestTypeId").Value = pTestID
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("ppatientId", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("ppatientId").Value = pPatientID
            End With
            Dim dt As New DataTable
            Dim ad As New SqlDataAdapter(cmd)
            ad.Fill(dt)
            If dt.Rows.Count > 0 Then
                pTestID = dt.Rows(0).Item("TEST_ID")
                pPatientID = dt.Rows(0).Item("PATIENT_ID")
                pBarcode = dt.Rows(0).Item("Barcode")
                pdeviceId = dt.Rows(0).Item("Device_ID")
            Else
                pTestID = 0
                pPatientID = 0
                pBarcode = 0
            End If
        Catch ex As Exception

        End Try
    End Sub

    Function spInsertTemp(ByVal pBarcode As String, _
                          ByVal pTestSeq As String, _
                          ByVal pTestTypeID As Int16, _
                          ByVal pTestDate As String, _
                          ByVal pDataSeq As Int16, _
                          ByVal pParaName As String, _
                          ByVal pTestResult As String, _
                          ByVal pMeasureUnit As String, _
                          ByVal pNormalLevel As String, _
                          ByVal pNormalLevelW As String) As Boolean
        Try
            Dim cmd As New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spInsertTemp" & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spInsertDataTemp"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pTestType_ID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pTestType_ID").Value = pTestTypeID
                .Parameters.Add("pTestSeq", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestSeq").Value = " " 'pTestSeq
                .Parameters.Add("pTestDate", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pTestDate").Value = pTestDate
                .Parameters.Add("pDataSeq", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pDataSeq").Value = pDataSeq
                .Parameters.Add("pParaName", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pParaName").Value = pParaName
                .Parameters.Add("pTestResult", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pTestResult").Value = pTestResult
                .Parameters.Add("pMeasureUnit", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pMeasureUnit").Value = IIf(pMeasureUnit Is Nothing, " ", pMeasureUnit)
                .Parameters.Add("pNormalLevel", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevel").Value = IIf(pNormalLevel Is Nothing, " ", pNormalLevel)
                .Parameters.Add("pNormalLevelW", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pNormalLevelW").Value = IIf(pNormalLevelW Is Nothing, " ", pNormalLevelW)
                .ExecuteNonQuery()
            End With
            cmd.Dispose()
        Catch ex As Exception

        End Try
    End Function

    ' Kiem tra xem Patient_ID nay da co chua
    Function spCheckBarcodeTemp(ByVal pBarcode As String, _
                    ByVal pParaName As String, _
                    ByRef pDetailID As Int64) As Boolean
        Try
            Dim cmd As New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spCheckBarcodeTemp" & vbCrLf)
            End Try

            Try
                With cmd
                    .Connection = methodConnection
                    .CommandText = "spCheckBarcodeTemp"
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add("pBarcode", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                    .Parameters("pBarcode").Value = pBarcode
                    .Parameters.Add("pParaName", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                    .Parameters("pParaName").Value = pParaName
                    .Parameters.Add("pReSult", SqlDbType.Int).Direction = ParameterDirection.Output
                    .Parameters.Add("pTestDetailID", SqlDbType.BigInt).Direction = ParameterDirection.Output
                    .ExecuteNonQuery()
                End With

            Catch ex As Exception

            End Try
            If cmd.Parameters("pReSult").Value > 0 Then
                pDetailID = cmd.Parameters("pTestDetailID").Value
                cmd.Dispose()
                Return True
            Else
                pDetailID = 0
                cmd.Dispose()
                Return False
            End If
        Catch ex As Exception

        End Try
    End Function

    Function spUpdateResultTemp(ByVal pTestDetailID As Int64, _
                                ByVal pBarcode As String, _
                                ByVal pNewResult As String) As Boolean
        Try
            Dim cmd As New SqlCommand

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\LOG\DB_Connection_Error.txt", Now.ToString & "     spUpdateResultTemp" & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spUpdateResultTemp"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pTestDetail_ID", SqlDbType.BigInt, 10).Direction = ParameterDirection.Input
                .Parameters("pTestDetail_ID").Value = pTestDetailID
                .Parameters.Add("pBarcode", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pTestResult", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pTestResult").Value = pNewResult
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Function Trim_All(ByVal vString As String) As String
        vString = Replace(vString, " ", "")
        Return vString
    End Function

    Function Get_Account_Hitachi717() As Integer
        Try
            If Not IO.File.Exists(Application.StartupPath & "\Account_Hitachi717.txt") Then Return 1
            Dim strRd As New IO.StreamReader(Application.StartupPath & "\Account_Hitachi717.txt")
            Dim s As String = strRd.ReadToEnd
            If s.Trim = "" Then Return 1
            Select Case s.Trim
                Case "1" 'Hai Phong
                    Return 1
                Case "2" 'NHTD
                    Return 2
                Case "3" 'Xay Dung
                    Return 3
                Case "4" '1C Viet Duc
                    Return 4
                Case Else
                    Return 3
            End Select
        Catch ex As Exception
            Return 3
        End Try
    End Function

    Function Get_Account_AU400() As Integer
        Try
            If Not IO.File.Exists(Application.StartupPath & "\Account_AU400.txt") Then Return 1
            Dim strRd As New IO.StreamReader(Application.StartupPath & "\Account_AU400.txt")
            Dim s As String = strRd.ReadToEnd
            If s.Trim = "" Then Return 1
            Select Case s.Trim
                Case "1" 'Hồng Ngọc
                    Return 1
                Case "2" 'NHTD
                    Return 2
            End Select
        Catch ex As Exception
            Return 1
        End Try
    End Function

    Function Get_Account_XT1800() As Integer
        Try
            If Not IO.File.Exists(Application.StartupPath & "\Account_XT1800.txt") Then Return 1
            Dim strRd As New IO.StreamReader(Application.StartupPath & "\Account_XT1800.txt")
            Dim s As String = strRd.ReadToEnd
            If s.Trim = "" Then Return 1
            Select Case s.Trim
                Case "1" 'Usual
                    Return 1
                Case "3" 'Xay Dung
                    Return 3
                Case "6" 'NHTD
                    Return 6
                Case Else
                    Return 1
            End Select
        Catch ex As Exception
            Return 1
        End Try
    End Function

    Private Function FindInResult(ByVal pDataName As String) As Integer
        Dim iresult As Integer = -1
        Try
            For id As Integer = 0 To Me.vResult.GetLength(1) - 1
                If vResult(0, id).Trim = pDataName.Trim Then
                    iresult = id
                    Exit For
                End If
            Next
        Catch ex As Exception
            'MsgBox(ex.ToString)
        End Try
        Return iresult
    End Function

End Class
